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

yipeiwu_com6年前PHP代码库
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;//(取值)
}*/

相关文章

php创建多级目录的方法

本文实例讲述了php创建多级目录的方法。分享给大家供大家参考。具体实现方法如下: <?php /* 写出一个能创建多级目录的PHP函数 */ function...

基于php双引号中访问数组元素报错的解决方法

最近在做微信公众号开发,在一个发送图文接口中,需要把数组元素拼接在XML字符串中 foreach ($itemArr as $key => $value){ $items...

php Undefined index和Undefined variable的解决方法

$act=$_POST['act']; 用以上代码总是提示 Notice: Undefined index: act in F:\win...

PHP7内核CGI与FastCGI详解

PHP7内核CGI与FastCGI详解

CGI:是 Web Server 与 Web Application 之间数据交换的一种协议。 FastCGI:同 CGI,是一种通信协议,但比 CGI 在效率上做了一些优化。 PHP-...

php下过滤HTML代码的函数

具体如下所示: /*---------------------- 过滤HTML代码的函数 -----------------------*/ function htmlEnco...