Home Backend Development PHP Tutorial Detailed explanation of php using cookies to store browsing records

Detailed explanation of php using cookies to store browsing records

Oct 20, 2016 pm 03:19 PM
php

When we are working on projects, some modules are often browsing record lists, but how do we implement the browsing record function in PHP? Now we implement it through cookies


Let’s first analyze the idea

Access The page

gets the product data list in the stored browsing history

If the currently browsed product is not in the history record, it will be added, and if it is, it will be updated

If you have a rough idea, you can look at the code directly


The complete code is as follows:

<?php
 
//如是COOKIE 里面不为空,则往里面增加一个商品ID
if (!empty($_COOKIE[&#39;SHOP&#39;][&#39;history&#39;])){
     
    //取得COOKIE里面的值,并用逗号把它切割成一个数组
    $history = explode(&#39;,&#39;, $_COOKIE[&#39;SHOP&#39;][&#39;history&#39;]);
     
    //在这个数组的开头插入当前正在浏览的商品ID
    array_unshift($history, $id);
     
    //去除数组里重复的值
    $history = array_unique($history);
    // $arr = array (1,2,3,1,3);
    // $arr = array (1,1,2,3,3);
    // $arr = array (1,2,3);
     
    //当数组的长度大于5里循环执行里面的代码
    while (count($history) > 5){
        //将数组最后一个单元弹出,直到它的长度小于等于5为止
        array_pop($history);
    }
 
 
    //把这个数组用逗号连成一个字符串写入COOKIE,并设置其过期时间为30天
    setcookie(&#39;SHOP[history]&#39;, implode(&#39;,&#39;, $history), $cur_time + 3600 * 24 * 30);
 
}else{
    //如果COOKIE里面为空,则把当前浏览的商品ID写入COOKIE ,这个只在第一次浏览该网站时发生
    setcookie(&#39;SHOP[history]&#39;, $id, $cur_time + 3600 * 24 * 30);
}
 
//以上均为记录浏览的商品ID到COOKIE里,下面将讲到怎样用这样COOKIE里的数据
 
//取得COOKIE里的数据 ,格式为1,2,3,4 这样,当然也有可以为0
$history =isset ($_COOKIE[&#39;SHOP&#39;][&#39;history&#39;]) ? $_COOKIE[&#39;SHOP&#39;][&#39;history&#39;] : 0;
 
//写SQL语句,用IN 来查询出这些ID的商品列表
$sql_history = "SELECT * FROM `goods` WHERE `goods_id` in ({$history})";
 
 
//执行SQL语句,返回数据列表
$goods_history = $db->getAll($sql_history);
if ($goods_history) {
    $tpl->assign (&#39;goods_history&#39;,$goods_history);
}
?>
Copy after login

The following is the mysql operation to obtain product information based on the storage ID. You can obtain it according to your own code, you can use the original ecology, or you can use the framework's query database method. You can modify it appropriately


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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles