A method for thinkphp to realize the function of browsing history

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.