Home  >  Article  >  Backend Development  >  About how thinkphp implements the browsing history function

About how thinkphp implements the browsing history function

不言
不言Original
2018-06-08 15:06:311701browse

This article mainly introduces the implementation method of thinkphp browsing history function. It can realize the browsing history function of the browser. It is a very practical skill. Friends who need it can refer to it.

The example of this article tells the thinkphp browsing history The function implementation method is shared 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 cookies. Let me introduce one to you. Example of implementing 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.

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

2. Add the code in function.php

/**
  +----------------------------------------------------------
 * 浏览记录按照时间排序
  +----------------------------------------------------------
 */
function my_sort($a, $b){
$a = substr($a,1);
$b = substr($b,1);
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
  }
/**
  +----------------------------------------------------------
 * 网页浏览记录生成
  +----------------------------------------------------------
 */
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));  //设置cookie
if (!cookie('history')){//cookie空,初始一个
cookie('history',$cookie_history);
}else{
$new_history = array_merge(cookie('history'),$cookie_history);//添加新浏览数据
uksort($new_history, "my_sort");//按照浏览时间排序
$history = array_unique($new_history);
if (count($history) > 4){
$history = array_slice($history,0,4);
}
cookie('history',$history);
}
}
/**
  +----------------------------------------------------------
 * 网页浏览记录读取
  +----------------------------------------------------------
 */
function cookie_history_read(){
$arr = cookie('history');
foreach ((array)$arr as $k => $v){
$list[$k] = json_decode($v,true);
}
return $list;
}

3. Output the information on the page that needs to display the browsing history

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

Just use volist to display it in the template.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Use ThinkPHP to generate thumbnails and display them

About thinkPHP’s method of implementing batch deletion

Analysis on ThinkPHP’s use of getlist method to implement data search function

The above is the detailed content of About how thinkphp implements the browsing history function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn