Php records the products that users have visited, and records the product id and id in cookies to obtain product information. 1. test method: www. xxx. comtest. php? Content_contentid]; 2. connection 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)
}*/
Why? Content_id = custom, such as: 44 code: $ content_id = array (); // 1. create an array $ content_id [] =$ _ GET ['tentid']; // 2. connection...