


Detailed explanation of php using cookies to store browsing records
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['SHOP']['history'])){ //取得COOKIE里面的值,并用逗号把它切割成一个数组 $history = explode(',', $_COOKIE['SHOP']['history']); //在这个数组的开头插入当前正在浏览的商品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('SHOP[history]', implode(',', $history), $cur_time + 3600 * 24 * 30); }else{ //如果COOKIE里面为空,则把当前浏览的商品ID写入COOKIE ,这个只在第一次浏览该网站时发生 setcookie('SHOP[history]', $id, $cur_time + 3600 * 24 * 30); } //以上均为记录浏览的商品ID到COOKIE里,下面将讲到怎样用这样COOKIE里的数据 //取得COOKIE里的数据 ,格式为1,2,3,4 这样,当然也有可以为0 $history =isset ($_COOKIE['SHOP']['history']) ? $_COOKIE['SHOP']['history'] : 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 ('goods_history',$goods_history); } ?>
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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