PHP Cookei code that records the user's historical browsing information, phpcookei

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.