欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 使用COOKIE制作浏览记录_PHP教程

程序员文章站 2022-05-19 14:55:14
...
文件1 cookieHistory.class.php
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

getData($data);



echo "
";
print_r($cookiedate);
?>

  程序流程:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440208.htmlTechArticle文件1cookieHistory.class.php ?php/** *使用COOKIE 制作网站浏览记录 *by threemore */class HistoryCookie {var $times =""; //记录COOKIE保存时间var $cookiename = 'Hist...