Home  >  Article  >  Backend Development  >  Record the products visited by the user in PHP, record the product ID in the cookie, and obtain the product information by ID_PHP Tutorial

Record the products visited by the user in PHP, record the product ID in the cookie, and obtain the product information by ID_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:36665browse

1. Test method www.xxx.com/test.php?content_id=define by yourself, such as: 44

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 it exists)
{
$now_content = str_replace("\" , "", $_COOKIE['content_id']);//(4). You can check the cookie. If there is a problem with unserialize, I removed the slash inside
$now = unserialize( $now_content); //(5). Deinstantiate 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 the array;
{
$content_id[] = $w; //(8). Insert into array
}
}
$content= serialize($content_id); //(9). Instantiate the array into String
setcookie("content_id",$content, time()+3600*24); //(10). Insert into cookie

}else {
$content= serialize($ content_id); //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)
}*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323398.htmlTechArticle1. Test method www.xxx.com/test.php?content_id=Defined by yourself, such as: 44 Copy code The code is as follows: $content_id = array(); //1. Create an array $content_id[] = $_GET['contentid']; //2. Connect...
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