This article tells the thinkphp browsing history function Realization method, shares for everybody reference. The specific implementation methods are analyzed as follows:
The history browsing function uses the cookie function to record the user information to the local, so we can only read the value which is stored in the cookies to be possible, below lets us introduce an example based on the thinkphp realization browsing history function.
Just like a browser, you can record which pages are accessed, so that you can reduce the time, and below we realize the function of browsing history.
1. In the product or news page where you need to record the browsing data, record the information that the cookie needs to be saved, such as the following line of code, pass the page ID, product name, price, thumbnail, url to cookie_history.
Copy Code code as follows:
Cookie_history ($id, $info [' title '], $info [' Price '], $info [' pic '], $thisurl);
Add code inside 2.function.php
Copy Code code as follows:
/**
+----------------------------------------------------------
* Browse records sorted by Time
+----------------------------------------------------------
*/
function My_sort ($a, $b) {
$a = substr ($a, 1);
$b = substr ($b, 1);
if ($a = = $b) return 0;
return ($a > $b)? -1:1;
}
/**
+----------------------------------------------------------
* Web browsing record generation
+----------------------------------------------------------
*/
function Cookie_history ($id, $title, $price, $img, $url) {
$dealinfo [' title '] = $title;
$dealinfo [' price '] = $price;
$dealinfo [' img '] = $img;
$dealinfo [' url '] = $url;
$time = ' t '. Now_time;
$cookie _history = Array ($time => json_encode ($dealinfo)); Set cookies
if (!cookie (' history ')) {//cookie null, initial one
Cookies (' History ', $cookie _history);
}else{
$new _history = array_merge (Cookie (' history '), $cookie _history);//Add New browsing data
Uksort ($new _history, "My_sort"); Sort by browse time
$history = Array_unique ($new _history);
if (count ($history) > 4) {
$history = Array_slice ($history, 0,4);
}
Cookies (' history ', $history);
}
}
/**
+----------------------------------------------------------
* Web browsing record reading
+----------------------------------------------------------
*/
function Cookie_history_read () {
$arr = Cookie (' history ');
foreach ((array) $arr as $k => $v) {
$list [$k] = Json_decode ($v, true);
}
return $list;
}
3. Page output information when you need to display a browsing record
Copy Code code as follows:
$this->assign (' History ', Cookie_history_read ());
The template inside with Volist to show out on the line.
I hope this article will help you with your PHP program design.