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

来源:互联网 发布:电商推荐算法 编辑:程序博客网 时间:2024/05/17 13:45
用户的浏览商品记录功能代码
  1. /**
  2.  * 车辆历史浏览记录
  3.  * $data 车辆记录信息
  4.  */
  5. protected function _history($data)
  6. {
  7.   if(!$data || !is_array($data))
  8.   {
  9.     return false;
  10.   }
  11.   //判断cookie类里面是否有浏览记录
  12.   if($_COOKIE['history_car'])
  13.   {
  14.     $history = unserialize($_COOKIE['history_car']);
  15.     array_unshift($history, $data); //在浏览记录顶部加入
  16.     /* 去除重复记录 */
  17.     $rows = array();
  18.     foreach ($history as $v)
  19.     {
  20.       if(in_array($v, $rows))
  21.       {
  22.         continue;
  23.       }
  24.       $rows[] = $v;
  25.     }
  26.     /* 如果记录数量多余5则去除 */
  27.     while (count($rows) > 5)
  28.     {
  29.       array_pop($rows); //弹出
  30.     }
  31.     setcookie('history_car',serialize($rows),time()+3600*24*30,'/');
  32.   }
  33.   else
  34.   {
  35.     $history = serialize(array($data));
  36.     setcookie('history_car',$history,time()+3600*24*30,'/');
  37.   }
  38. }
原创粉丝点击