Thinkphp browsing history implementation method, thinkphp history
This article describes how to implement the thinkphp browsing history function and shares it with you for your reference. The implementation method is analyzed as follows:
The history browsing function uses the cookie function to record user information and store it locally. In this way, we only need to read the value stored in cookies, the following is an example of the browsing history function based on thinkphp.
Just like a browser, it can record the pages accessed, which can reduce the time. Next we will implement the browsing history function.
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.
Copy codeThe Code is as follows: cookie_history ($ id, $ info ['title'], $ info ['price'], $ info ['pic '], $ thisurl );
2. Add code in function. php
Copy codeThe Code is as follows :/**
+ ----------------------------------------------------------
* 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
Copy codeThe Code is as follows: $ this-> assign ('History ', cookie_history_read ());
In the template, use volist to display it.
I hope this article will help you with PHP programming.
Thinkphp implements the most basic functions
This is an action class. The last sentence $ this-> display ('Public: text') is to display the text Template under Public. That is to say, first execute this method of this class and then display the template. The variables used by the template are allocated by calling assign here. For example, $ this-> assign ('time', time (); is to allocate the current time to the template using the time name. The html code in else should be written to the template.
How can thinkphp achieve this?
The front-end is a multi-choice box, and then in the Controller to determine whether the multi-choice box is selected, select the where syntax. It is usually $ this-> dao-> where ("only show goods = $ ")