search
Homephp教程PHP源码PHP Cookie记录用户历史浏览信息例子

Cookie我们经常使用了可以记录到用户的行为并且保存信息在用户本地了,下次访问还可以直接调用这些Cookie信息了,下面我们来看一个利用PHP Cookie记录用户历史浏览信息例子,希望文章对各位有帮助。

<script>ec(2);</script>


购物类网站有时候需要了解用户最近的需求,收集和推送用户的查看记录成为了大部分此类的网站不可缺少的功能,下面是本人在开发项目中自己写的方法,一来留有保存,二来和大家分享!

【基础】

Cookie常用方法:

$_COOKIE[‘RecordLuHuiDUDU’] 得到Cookie
setcookie(‘RecordLuHuiDUDU’,”,time()-3600*24*30); setcookie(字段名,数据,过期时间); 设置Cookie

【注意】

重新设置Cookie需要使之前的Cookie失效,删除Cookie也是同样的方法:

setcookie(‘RecordLuHuiDUDU’,”,time()-3600*24*30);

【代码示例】

/**
     * 将用品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;
}

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!