1. Test method: www.xxx.com/test.php? Content_id = custom, for example, 44
Copy codeThe 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)
}*/