How to implement thinkphp browsing history function, thinkphp history
The example in this article describes how to implement the thinkphp browsing history function and shares it with everyone for your reference. The specific implementation method is analyzed as follows:
The historical 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 the cookie. Here is an example of implementing the browsing history function based on thinkphp.
Just like a browser, it can record which pages have been visited, which can reduce time. Next, we implement the browsing history function.
1. On the product or news page where you need to record browsing data, record the information that the cookie needs to save. For example, in the following line of code, pass the page ID, product name, price, thumbnail, and URL to cookie_history.
Copy code The code is as follows:
cookie_history($id,$info['title'],$info['price'], $info['pic'],$thisurl);
2.Add code in function.php
Copy code The code is as follows:
/**
+------------------------------------------------- ----------
* Browsing 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;
}
/**
+------------------------------------------------- ----------
* 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')){//Empty cookie, initial one
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);
}
}
/**
+------------------------------------------------- ----------
* Read web browsing history
+------------------------------------------------- ----------
*/
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 where browsing history needs to be displayed
Copy code The code is as follows:
$this->assign('history',cookie_history_read());
Just use volist to display it in the template.
I hope this article will be helpful to everyone’s PHP programming design.
This is an action class, and the last sentence $this->display('Public:text'); is to display the text template under public. That is to say, execute this method of this class first, and then display the template. The variables used by the template are assigned by calling assign from here. For example, $this->assign('time',time()); assigns the current time to the template using the time name. The html code in else should be written into the template.
The front desk is a multi-select box, and then in the Controller, you can determine whether the multi-select box is selected, and just use the where syntax to select it. Usually $this->dao->where("Only show availability=$a")
http://www.bkjia.com/PHPjc/902779.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/902779.htmlTechArticleThinkphp browsing history function implementation method, thinkphp history This article describes the implementation method of thinkphp browsing history function and shares it with everyone. For everyone’s reference. The specific implementation method is analyzed as follows:...