Home > php教程 > php手册 > php cookie记录用户访问过的网页方法

php cookie记录用户访问过的网页方法

WBOY
Release: 2016-05-25 16:57:02
Original
940 people have browsed it

cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 php教程,您能够创建并取回 cookie 的值。
如何创建 cookie?
setcookie() 函数用于设置 cookie。

注释:setcookie() 函数必须位于 标签之前。

语法
setcookie(name, value, expire, path, domain);

$content_id = array();//1.创建一个数组 
$content_id[] = $_get['contentid']; //2.对接受到的id插入到数组中去
if(isset($_cookie['content_id'])) //3.判定cookie是否存在,第一次不存在(如果存在的话) 
{ 
$now_content = str_replace("", "", $_cookie['content_id']);//(4).您可以查看下cookie,此时如果unserialize的话出问题的,我把里面的斜杠去掉了 
$now = unserialize($now_content); //(5).把cookie 中的serialize生成的字符串反实例化成数组 
foreach($now as $n=>$w) { //(6).里面很多元素,所以我要foreach 出值 
if(!in_array($w,$content_id)) //(7).判定这个值是否存在,如果存在的化我就不插入到数组里面去; 
{ 
$content_id[] = $w; //(8).插入到数组 
} 
} 
$content= serialize($content_id); //(9).把数组实例化成字符串 
setcookie("content_id",$content, time()+3600*24); //(10).插入到cookie
}else { 
$content= serialize($content_id);//4.把数组实例化成字符串 
setcookie("content_id",$content, time()+3600*24); //5.生成cookie 
}
$getcontent = unserialize(str_replace("", "", $_cookie['content_id'])); 
/*foreach($getcontent as $row=>$r) 
{ 
echo $r;//(取值) 
}*/
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template