: This article mainly introduces the code for PHPCookei to record historical user browsing information. if you are interested in PHP tutorials, refer to it. [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
The above describes the PHP Cookei code for recording the historical browsing information of users, including some content, and hope to help friends who are interested in PHP tutorials.