View the browsing history of a specific web page

Source: Internet
Author: User
Tags set cookie
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn the browsing history function similar to t.58.com to view the records of your group buying products.
1. on the product or news page where you want to record the data browsing, record the information that the cookie needs to save, such as the following line of code, and pass the page ID, product name, price, thumbnail, and URL to cookie_history. cookie_history($id,$info['title'],$info['price'],$info['pic'],$thisurl);2. Add code in function. php /**
+ ----------------------------------------------------------
* Browsing records are 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;
}
/**
+ ----------------------------------------------------------
* Generate Web browsing records
+ ----------------------------------------------------------
*/
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 cookie
If (! Cookie ('History ') {// The cookie is empty.
Cookie ('History ', $ cookie_history );
} Else {
$ New_history = array_merge (cookie ('History '), $ cookie_history); // Add new browsing data
Uksort ($ new_history, "my_sort"); // sort by browsing time
$ History = array_unique ($ new_history );
If (count ($ history)> 4 ){
$ History = array_slice ($ history, 0, 4 );
}
Cookie ('History ', $ history );
}
}
/**
+ ----------------------------------------------------------
* Reading web browsing records
+ ----------------------------------------------------------
*/
Function cookie_history_read (){
$ Arr = cookie ('History ');
Foreach (array) $ arr as $ k => $ v ){
$ List [$ k] = json_decode ($ v, true );
}
Return $ list;
}
3. Output Information on the page for displaying browsing records $this->assign('history',cookie_history_read());In the template, use volist to display it.
If you have a better way to obtain browsing records, post them below for me to learn.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.