Home Backend Development PHP Tutorial 用php实现类似淘宝,拍拍,易趣的最近浏览商品的功能模型代码,该如何处理

用php实现类似淘宝,拍拍,易趣的最近浏览商品的功能模型代码,该如何处理

Jun 13, 2016 am 10:03 AM
quot setcookie

用php实现类似淘宝,拍拍,易趣的最近浏览商品的功能模型代码
最近在做一个项目,要用到类似淘宝,拍拍,易趣的最近浏览商品的功能,在网络上遍寻不及,遂找出一ASP代码用PHP自行改写,得以下片段,先发上来和大家共享。此代码片段只包含cookie写入部分,读出处理的部分大家可以自行编写。最先跟帖提出建议的给以加分。

/*
RecentlyGoods File
Urchin Studio FoxCMS
Version: 1.1.6
Author: luckyfox ([email protected])
Copyright: Urchin Studio (www.itocean.net)
Last Modified: 2008-9-3

*/

//TempNum 显示临时记录数

$TempNum=5; 

//setcookie("RecentlyGoods", "12,31,90,39");

//RecentlyGoods 最近商品RecentlyGoods临时变量

if (isset($_COOKIE['RecentlyGoods']))
{
$RecentlyGoods=$_COOKIE['RecentlyGoods'];
$RecentlyGoodsArray=explode(",", $RecentlyGoods);
$RecentlyGoodsNum=count($RecentlyGoodsArray); //RecentlyGoodsNum 当前存储的变量个数

}

if($_GET['Id']!="")
{

$Id=$_GET['Id']; //ID 为得到请求的字符


//如果存在了,则将之前的删除,用最新的在尾部追加

if (strstr($RecentlyGoods, $Id)) 
{
//echo "已经存在,则不写入COOKIES


";
}
else
{
if($RecentlyGoodsNum {
if($RecentlyGoods=="")
{
setcookie("RecentlyGoods",$Id,time()+3600);
}
else
{
$RecentlyGoodsNew=$RecentlyGoods.",".$Id;
setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600);
}
}
else //如果大于了指定的大小后,将第一个给删去,在尾部再加入最新的记录。
{
$pos=strpos($RecentlyGoods,",")+1; //第一个参数的起始位置
$FirstString=substr($RecentlyGoods,0,$pos); //取出第一个参数
$RecentlyGoods=str_replace($FirstString,"",$RecentlyGoods); //将第一个参数删除
$RecentlyGoodsNew=$RecentlyGoods.",".$Id; //在尾部加入最新的记录
setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600);
}

}


}
?>


------解决方案--------------------
收藏。谢谢
------解决方案--------------------
学习..收藏。
------解决方案--------------------
up
------解决方案--------------------

------解决方案--------------------
up
------解决方案--------------------
jf
------解决方案--------------------
看看
------解决方案--------------------
支持.
------解决方案--------------------
if (strstr($RecentlyGoods, $Id)) 

//echo "已经存在,则不写入COOKIES
"; 
}

如果我看了100号,100保存了,然后我再看10号,那就不能保存了
------解决方案--------------------
很好,收藏了。。
------解决方案--------------------
学习..收藏。

------解决方案--------------------
对id的处理用字符串查找来处理,这样的方式其实不太合理。
PHP code
define('MAX_ITEMS', 5);$id = intval($_GET['id']);  // 确保id是整数if (isset($_COOKIE['recentlyGoods'])) {  // 已有历史数据,追加新数据  $goods = explode(',', $_COOKIE['recentlyGoods']);  // 取得id数组  $key = array_search($id, $goods); // 在历史数据中查找当前id  if ($key !== false) {    unset($goods[$key]);  // 找到则删除  }  $goods[] = $id; // 追加当前浏览的id} else {  // 尚未有历史数据,创建并将当前浏览id加入  $goods = array($id);}// 如果数组中历史数据条目超出限制,则截取最后一段if (count($goods) > MAX_ITEMS) {  // 从倒数第MAX_ITEMS条开始提取MAX_ITEMS条记录  $goods = array_slice($goods, 0 - MAX_ITEMS, MAX_ITEMS);}setcookie('recentlyGoods', join(',', $goods), time()+3600);<div class="clear">
                 
              
              
        
            </div>
Copy after login
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

Video Face Swap

Video Face Swap

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

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)

What to do if setcookie php fails What to do if setcookie php fails Oct 26, 2022 am 09:07 AM

Solution to the failure of setcookie php: 1. Open the corresponding PHP file; 2. Check whether the domain name parameters are standardized; 3. Implement it through global variables, code such as "foreach ($_COOKIE["cookie"] as $name => $value) {$name = htmlspecialchars($name);$value = htmlspecialchars($value);echo...".

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to use the setcookie function to set cookies in PHP How to use the setcookie function to set cookies in PHP Jun 26, 2023 pm 12:00 PM

In web development, cookies are a very common technology that allow web applications to store and access data on the client side. In PHP programming, setting cookies is usually implemented using the setcookie function. The syntax of the setcookie function is as follows: boolsetcookie(string$name[,string$value[,int$expire[,string$path[,

What does the timestamp of 0 in php setcookie mean? What does the timestamp of 0 in php setcookie mean? Mar 22, 2023 am 10:38 AM

The setcookie() function in PHP is a way to set a cookie to the client browser. It allows the website to recognize the user through cookies on the next visit and provide them with corresponding services. setcookie() has several parameters, including cookie name, value, expiration time, etc. When setting the expiration time, it can be specified by timestamp. However, sometimes we see timestamps being set to 0. What does this mean?

Use the PHP function 'setcookie' to set cookies Use the PHP function 'setcookie' to set cookies Jul 25, 2023 am 09:22 AM

Set cookies using the PHP function "setcookie" In website development, cookies are a very common technology that is used to store a small amount of data in the user's browser in order to pass information between different pages. PHP provides a function called "setcookie" for setting cookie values ​​and attributes. In this article, we will learn how to set cookies using the "setcookie" function. The following is the use of "setcook

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

图片消失怎么解决 图片消失怎么解决 Apr 07, 2024 pm 03:02 PM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

图片消失怎么解决 图片消失怎么解决 Jun 13, 2016 am 10:09 AM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

See all articles