Home  >  Article  >  Backend Development  >  php中记录用户访问过的产品,在cookie记录产品id,id取得产品信息_PHP

php中记录用户访问过的产品,在cookie记录产品id,id取得产品信息_PHP

WBOY
WBOYOriginal
2016-06-01 12:16:38818browse

Cookie

1.测试方法www.xxx.com/test.php?content_id=自己定义,如:44
复制代码 代码如下:
$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;//(取值)
}*/
Statement:
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