thinkphp Browsing History Function Implementation Method _php example

Source: Internet
Author: User

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.

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.