This article mainly introduced the Thinkphp browsing history function Realization method, can realize the browser browsing history function, is very practical skill, needs the friend can refer to the next
The example of this article describes the Thinkphp browsing history function Realization method, share for everyone for reference. The specific implementation method is analyzed as follows:
The history browsing function uses the cookie function to log the user's information to the local, so that we just read the value stored in the cookie can be, the following is to introduce an example based on thinkphp to implement the browsing history function.
Just like a browser, you can record which pages are accessed, which reduces the time, and below we realize the function of browsing history.
1. In the product or news page where you need to record your browsing data, record the information that the cookie needs to be saved, such as the following line of code, which passes the page ID, product name, price, thumbnail, and URL to cookie_history.
Cookie_history ($id, $info [' title '], $info [' Price '], $info [' pic '], $thisurl);
2.function.php inside Add code
/** +----------------------------------------------------------* 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 Cookieif (!cookie (' history ')) {//cookie null, initial a cookie (' 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);} Cookie (' history ', $history);}} /** +----------------------------------------------------------* Web browsing record Read +----------------------------------------------------------*/ function Cookie_history_read () {$arr = Cookie (' history '), foreach ((array) $arr as $k = + $v) {$list [$k] = Json_decode ($v, true);} return $list;}
3. The page output information that needs to display the browsing history
$this->assign (' History ', Cookie_history_read ());
The template is displayed with Volist.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!