Home >Backend Development >PHP Tutorial >PHP Cookei's code for recording user history browsing information, phpcookei_PHP tutorial

PHP Cookei's code for recording user history browsing information, phpcookei_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:59:21870browse

PHP Cookei’s code for recording user history browsing information, phpcookei

[Basics]

Common methods for cookies:

$_COOKIE['RecordLuHuiDUDU'] gets Cookie
setcookie('RecordLuHuiDUDU',",time()-3600*24*30); setcookie(field name, data, expiration time); Set Cookie

【Attention】

Resetting cookies requires invalidating previous cookies. The same method is used to delete cookies:
setcookie(‘RecordLuHuiDUDU’,”,time()-3600*24*30);

【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( 'id' => $id,
            'time' => date('Y-m-d H:i:s', time())
          );
        }
        if($num <10){ $data[$num] = array( 'id' => $id,
            'time' => date('Y-m-d H:i:s', time())
          );
        }else {
          return false;
        }
      }
    }
    setcookie('RecordLuHuiDUDU',json_encode($data),time()+3600*24*30);
    return true;
  }

This article is from IT985 blog

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1098977.htmlTechArticlePHP Cookei code to record user historical browsing information, phpcookei [Basics] Commonly used cookie methods: $_COOKIE['RecordLuHuiDUDU' ] Get Cookie setcookie('RecordLuHuiDUDU',",time()...
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