Record the products accessed by users in php, and record the product id and id in cookies to obtain product information.

Source: Internet
Author: User
Php records the products accessed by users, and records product IDs and IDs in cookies to obtain product information. For more information, see. Cookie

1. test method: www.xxx.com/test.php? Content_id = custom, for example, 44
The code is as follows:
$ Content_id = array (); // 1. create an array
$ Content_id [] = $ _ GET ['tentid']; // 2. Insert the ID to the array.

If (isset ($ _ COOKIE ['content _ id']) // 3. determine whether the cookie exists. it does not exist for the first time (if so)
{
$ Now_content = str_replace ("\", "", $ _ COOKIE ['content _ id']); // (4 ). you can view the cookie. in this case, if unserialize is faulty, I have removed the slash in it.
$ Now = unserialize ($ now_content); // (5). reverse the string generated by serialize in the cookie into an array.
Foreach ($ now as $ n => $ w) {// (6). There are many elements in it, so I want foreach to output values.
If (! In_array ($ w, $ content_id) // (7). determine whether the value exists. if it exists, I will not insert it into the array;
{
$ Content_id [] = $ w; // (8). Insert to array
}
}
$ Content = serialize ($ content_id); // (9). convert the array instance into a string
Setcookie ("content_id", $ content, time () + 3600*24); // (10). Insert to cookie

} Else {
$ Content = serialize ($ content_id); // 4. convert the array instance into a string
Setcookie ("content_id", $ content, time () + 3600*24); // 5. generate cookie
}

$ Getcontent = unserialize (str_replace ("\", "", $ _ COOKIE ['content _ id']);
/* Foreach ($ getcontent as $ row => $ r)
{
Echo $ r; // (optional)
}*/

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.