Home >Backend Development >PHP Tutorial >The products visited by the user are recorded in PHP, the product ID is recorded in the cookie, and the product information is obtained by ID.

The products visited by the user are recorded in PHP, the product ID is recorded in the cookie, and the product information is obtained by ID.

WBOY
WBOYOriginal
2016-07-29 08:45:011062browse

1. Test method www.xxx.com/test.php?c

Copy code The code is as follows:


$content_id = array(); //1. Create an array
$content_id[] = $ _GET['contentid']; //2. Insert the received ID into the array
if(isset($_COOKIE['content_id'])) //3. Determine whether the cookie exists. It does not exist for the first time (if If exists)
{
$now_content = str_replace("\", "", $_COOKIE['content_id']);//(4). You can check the cookie. If there is a problem with unserialize, I will The slashes inside are removed
$now = unserialize($now_content); //(5).De-instantiate 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 need to foreach the value
if(!in_array($w,$content_id)) //(7). Determine whether this value exists. If it exists, I will not insert it into it. into the array;
{
$content_id[] = $w; //(8). Insert into the array
}
}
$c //(9). Instantiate the array into a string
setcookie("content_id", $content, time()+3600*24); //(10). Insert into cookie
}else {
$c//4. Instantiate the array 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;//(value)
}*/

The above introduces how to record the products visited by users in PHP, record the product ID in the cookie, and obtain the product information by ID, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn