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
Release: 2016-07-21 15:29:36
Original
675 people have browsed it

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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!