PHP Cookei code that records the user's historical browsing information, phpcookei
[Basics]
Common Cookie methods:
$ _ COOKIE ['recordluhuidudu'] to get the Cookie
Setcookie ('recordluidudu', ", time ()-3600*24*30); setcookie (field name, Data, expiration time); Set Cookie
Note]
To reset a Cookie, you need to invalidate the previous Cookie. The same method is also used to delete the Cookie:
Setcookie ('recordluidudu', ", time ()-3600*24*30 );
[Sample Code]
/*** Save the product id to the 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 ['recordluidudu']; setcookie ('recordluidudu ', '', time ()-3600*24*30 ); $ data = json_decode ($ data, true); $ num = count ($ data); // judge whether the request is repeated $ 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 ('recordluidudu', json_encode ($ data), time () + 3600*24*30 ); return true ;}
This article is from the IT985 blog