Implementation method of browsing history function in thinkphp

  • 2021-07-24 10:16:02
  • OfStack

This article tells the example of thinkphp browsing history function implementation method, and shares it with you for your reference. The specific implementation method is analyzed as follows:

History browsing function uses cookie function to record user information and put it locally, so we only need to read the value stored in cookies. Let's introduce an example of browsing history function based on thinkphp.

Just like Browser 1, it can record which pages are visited, which can reduce time. Let's realize the function of browsing history.

1. On the product or news page where you need to record browsing data, record the information that cookie needs to save, such as the following line of code, and pass the page ID, product name, price, thumbnail and website address to cookie_history.

cookie_history($id,$info['title'],$info['price'],$info['pic'],$thisurl);

2. Add code to function. php

/**
  +----------------------------------------------------------
 * Browse 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;
  }
/**
  +----------------------------------------------------------
 * 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));  // Settings cookie
if (!cookie('history')){//cookie Empty, initial 1 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 browsing time
$history = array_unique($new_history);
if (count($history) > 4){
$history = array_slice($history,0,4);
}
cookie('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. Output information on the page where you want to display browsing records

$this->assign('history',cookie_history_read());

The template is displayed with volist.

I hope this article is helpful to everyone's PHP programming.


Related articles: