Home > Backend Development > PHP Tutorial > How to record user browsing history in PHP

How to record user browsing history in PHP

墨辰丷
Release: 2023-03-29 19:14:02
Original
2271 people have browsed it

This article mainly introduces how PHP records user history browsing. Interested friends can refer to it. I hope it will be helpful to everyone.

Code example:

/**
   * 将用品id存入Cookie中
   *
   * @param $id
   * @return bool
   */
  public function setCookieRecord($id){
    $data = null;
    if(!isset($_COOKIE['RecordLuHuiDUDU'])){
      if(!empty($id)) {
        $data[0] = array(
          'id' = $id,
          'time' = date('Y-m-d H:i:s', time())
        );
      }else{
        return false;
      }
    }else{
      if(!empty($id)) {
        $data = $_COOKIE['RecordLuHuiDUDU'];
        setcookie('RecordLuHuiDUDU','',time()-3600*24*30);
        $data = json_decode($data, true);
        $num = count($data);
        //判断是否重复
        $judge = false;
        foreach($data as $index => $value){
          if($data[$index]['id'] == $id){
            $data[$index]['time'] = date('Y-m-d H:i:s', time());
            $judge = true;
          }
        }
 
        if($judge){
          setcookie('RecordLuHuiDUDU',json_encode($data),time()+3600*24*30);
          return true;
        }
 
        if($num == 10){
          for($i = 0; $i < 9; $i++){ $data[$i] = $data[$i+1]; } $data[9] = array( &#39;id&#39; => $id,
            &#39;time&#39; => date(&#39;Y-m-d H:i:s&#39;, time())
          );
        }
        if($num <10){ $data[$num] = array( &#39;id&#39; => $id,
            &#39;time&#39; => date(&#39;Y-m-d H:i:s&#39;, time())
          );
        }else {
          return false;
        }
      }
    }
    setcookie(&#39;RecordLuHuiDUDU&#39;,json_encode($data),time()+3600*24*30);
    return true;
  }
Copy after login

Summary: The above is the entire content of this article , I hope it can be helpful to everyone’s study.

Related recommendations:

PHP method of sending AT commands and example code

php array function array_walk usage and examples

php quick sorting principle and implementation method and example analysis

The above is the detailed content of How to record user browsing history in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template