Home  >  Article  >  Backend Development  >  How to browse history in php

How to browse history in php

墨辰丷
墨辰丷Original
2018-06-12 17:22:182335browse

This article mainly introduces the method of browsing history in PHP, involving the skills of operating cookies in PHP. It is of great practical value. Friends in need can refer to it.

The example of this article describes the method of browsing history in PHP. .

The specific implementation method is as follows:

/**
 * 商品历史浏览记录
 * $data 商品记录信息
 */
private function _history($data)
{
  if(!$data || !is_array($data))
  {
    return false;
  }
  //判断cookie类里面是否有浏览记录
  if($this->_request->getCookie('history'))
  {
    $history = unserialize($this->_request->getCookie('history'));
    array_unshift($history, $data); //在浏览记录顶部加入
    /* 去除重复记录 */
    $rows = array();
    foreach ($history as $v)
    {
      if(in_array($v, $rows))
      {
        continue;
      }
      $rows[] = $v;
    }
    /* 如果记录数量多余5则去除 */
    while (count($rows) > 5)
    {
      array_pop($rows); //弹出
    }
    setcookie('history',serialize($rows),time()+3600*24*30,'/');
  }
  else
  {
    $history = serialize(array($data));
    setcookie('history',$history,time()+3600*24*30,'/');
  }
}

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

PHP example analyzes the usage skills of mysql transaction processing

php implements the detection of the end tag in the html tag With the completion function

PHP implements clearing pictures that have not been accessed within a fixed date

The above is the detailed content of How to browse history in php. 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