首頁 > php教程 > php手册 > 主體

php 使用COOKIE制作浏览记录

WBOY
發布: 2016-06-13 11:31:01
原創
1562 人瀏覽過

文件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 class="brush:php;toolbar:false">";
print_r($cookiedate);
?>
登入後複製

  程序流程:

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板