PHP使用cookie做浏览历史记录
/** * @param $article文章详情 * @param int $count记录数 * tp须引入cookie类 */ function addHistory($article,$count=5) { if(!Cookie::has('history')){ Cookie::set('history',''); } $history=unserialize(Cookie::get('history')); $id=$article['id']; if(isset($history[$id])){ unset($history[$id]); } $history[$id]=$article['title'];//文章id作为键 if(count($history)>$count){ unset($history[key($history)]); } Cookie::set('history',serialize($history)); } /** * @return array|mixed|string获得历史记录 */ function getHistory() { if(!Cookie::has('history')){ return ''; } $history=unserialize(Cookie::get('history')); $history=array_reverse($history,true);//倒序 return $history; }