欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

用户的浏览商品记录功能代码

程序员文章站 2024-01-27 13:51:28
...
用户的浏览商品记录功能代码
/**
* 车辆历史浏览记录
* $data 车辆记录信息
*/
protected function _history($data)
{
if(!$data || !is_array($data))
{
return false;
}
//判断cookie类里面是否有浏览记录
if($_COOKIE['history_car'])
{
$history = unserialize($_COOKIE['history_car']);
array_unshift($history, $data); //在浏览记录顶部加入
/* 去除重复记录 */
$rows = array();
foreach ($history as $v)
{
if(in_array($v, $rows))
{
continue;
}
$rows[] = $v;
}
/* 如果记录数量多余5则去除 */
while (count($rows) > 5)
{
array_pop($rows); //弹出
}
setcookie('history_car',serialize($rows),time()+3600*24*30,'/');
}
else
{
$history = serialize(array($data));
setcookie('history_car',$history,time()+3600*24*30,'/');
}
}

AD:真正免费,域名+虚机+企业邮箱=0元