<?php //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 <hr />"; } else { if($RecentlyGoodsNum <$TempNum) //若是COOKIES中的元素小於指定的大小,則直接進行輸入COOKIES { 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); } } } ?>