2018年9月7日 星期五

【PHP】瀏覽過的商品

function save_history($id,$max=10)
{
 $t = 60 * 60 * 24 * 365;
 if ($_COOKIE['history_goods'])
 {
  $history_goods_arr = json_decode($_COOKIE['history_goods'],true);
  if (in_array($id,$history_goods_arr))
  {
   return false;
  }
  if (count($history_goods_arr) >= $max)
  {
   array_shift($history_goods_arr);
  }
  $history_goods_arr[] = $id;

  setcookie("history_goods",'',time()-3600);
  setcookie("history_goods",$history_goods,time()+$t);

 } else {
  $history_goods_arr[] = $id;
  $history_goods = json_encode($history_goods_arr);
  setcookie("history_goods",$history_goods,time()+$t);
 }

}

沒有留言:

張貼留言