Products visited by users are recorded in php and products id and id are recorded in cookie to obtain product information

  • 2020-05-05 11:02:40
  • OfStack

1. Test methods www. xxx. com/test php? content_id= self-defined, such as :44
 
$content_id = array();//1. Create an array  
$content_id[] = $_GET['contentid']; //2. Butt received ID Insert it into the array  

if(isset($_COOKIE['content_id'])) //3. determine cookie If there is a , The first time it didn't exist ( If it exists ) 
{ 
$now_content = str_replace("\\", "", $_COOKIE['content_id']);//(4). You can check it out cookie, If at this time unserialize If something goes wrong , I took the slash out of it  
$now = unserialize($now_content); //(5). the cookie  In the serialize The generated string is uninstantiated into an array  
foreach($now as $n=>$w) { //(6). There are a lot of elements in it , So I want to foreach  The value of  
if(!in_array($w,$content_id)) //(7). Determine if this value exists , If it exists, I'm not going to insert it into the array ; 
{ 
$content_id[] = $w; //(8). Insert into array  
} 
} 
$content= serialize($content_id); //(9). Instantiate an array as a string  
setcookie("content_id",$content, time()+3600*24); //(10). Inserted into the cookie 

}else { 
$content= serialize($content_id);//4. Instantiate an array as 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;//( The values ) 
}*/ 

Related articles: