$id = $_GET['id'];
$TempNum = 4;//cookie里面存储多少个浏览记录
if(isset($_COOKIE['RecentlyGoods']))//判断是否设置了COOKIE
{
$RecentlyGoods=$_COOKIE['RecentlyGoods'];
$RecentlyGoodsArray=explode(",", $RecentlyGoods);
$RecentlyGoodsNum=count($RecentlyGoodsArray); //RecentlyGoodsNum 当前存储的变量个数
if (in_array($id, $RecentlyGoodsArray))
{
//echo "已经存在,则不写入COOKIES
";
}
else
{
if($RecentlyGoodsNum
{
if($RecentlyGoods=="")
{
setcookie("RecentlyGoods", $id, time()+3600*5, '/');
}
else
{
$RecentlyGoodsNew=$RecentlyGoods.",".$id;
setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600*5, '/');
}
}
else //如果大于了指定的大小后,将第一个给删去,在尾部再加入最新的记录。
{
$pos=strpos($RecentlyGoods,",")+1; //第一个参数的起始位置
$FirstString=substr($RecentlyGoods,0,$pos); //取出第一个参数
$RecentlyGoods=str_replace($FirstString,"",$RecentlyGoods); //将第一个参数删除
$RecentlyGoodsNew=$RecentlyGoods.",".$id; //在尾部加入最新的记录g
setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600*5,'/');
}
}
}
else
{
setcookie("RecentlyGoods",$id,time()+3600*5, '/');
}
echo $_COOKIE['RecentlyGoods'];//取出访问的ID值 24,26,80,27
?>