文件1 cookieHistory.class.php
<?php /** *使用COOKIE 制作網站浏覽記錄 *by threemore */ class HistoryCookie { var $times =""; //記錄COOKIE保存時間 var $cookiename = 'History_cookie'; //COOKIE名稱 var $counts = 5; function __construct($name="",$times = '',$counts) { if(!empty($times)) $this->times = time()+$times; if(!empty($name)) $this->cookiename = $name; if(!empty($counts)) $this->counts = $counts; } //保存記錄到COOKIE中 public function getData($data) { $historydate = array(); $historydate[] = $data; //unset($_COOKIE[$this->cookiename]); if(isset($_COOKIE[$this->cookiename])) { $new_history = stripslashes($_COOKIE[$this->cookiename]); $new = unserialize($new_history); if(count($new) > ($this->counts-1)) return unserialize(stripslashes($_COOKIE[$this->cookiename])); foreach ($new as $key => $value) { if(!in_array($value,$historydate)) { $historydate[] =$value; } } $savedate = serialize($historydate); setcookie($this->cookiename,$savedate,time()+$this->times); }else { $savedate= serialize($historydate); setcookie($this->cookiename,$savedate,$this->times); } return unserialize(stripslashes($_COOKIE[$this->cookiename])); } //銷毀歷史記錄 public function Destroy() { unset($_COOKIE[$this->cookiename]); } } ?>
文件二 history.php
<?php require_once 'cookieHistory.class.php'; ob_start();//打開緩沖區 $history = new HistoryCookie('cookiename',10000); $data['id'] = $_GET['id']; $data['name'] = $_GET['name']; $cookiedate = $history->getData($data); echo "<pre>"; print_r($cookiedate); ?>
程序流程: