Shopping site sometimes need to understand the user's recent needs, collection and push the user's view record has become most of this kind of site indispensable functions, the following is my own in the development of the project to write their own methods, one to save, and then share with you!
Basis
Common Methods for cookies:
$_cookie[' Recordluhuidudu '] get cookies
Setcookie (' Recordluhuidudu ', ", Time () -3600*24*30); Setcookie (field name, data, expiration time); Set cookies
Attention
The same way to delete cookies is to reset the cookie before it expires:
Setcookie (' Recordluhuidudu ', ", Time () -3600*24*30);
"code Example"
/**
* Store supplies ID in cookies
*
* @param $id
* @return BOOL
*/
Public Function Setcookierecord ($id) {
$data = null;
if (!isset ($_cookie[' Recordluhuidudu ')) {
if (!empty ($id)) {
$data [0] = Array (
' id ' = $id,
' Time ' = Date (' y-m-d h:i:s ', Time ())
);
}else{
return false;
}
}else{
if (!empty ($id)) {
$data = $_cookie[' Recordluhuidudu '];
Setcookie (' Recordluhuidudu ', ', ', Time () -3600*24*30);
$data = Json_decode ($data, true);
$num = count ($data);
Judge whether to repeat
$judge = false;
foreach ($data as $index => $value) {
if ($data [$index] [' id '] = = $id) {
$data [$index] [' time '] = Date (' y-m-d h:i:s ', Time ());
$judge = true;
}
}
if ($judge) {
Setcookie (' Recordluhuidudu ', Json_encode ($data), Time () +3600*24*30);
return true;
}
if ($num = = 10) {
for ($i = 0; $i < 9; $i + +) {$data [$i] = $data [$i +1];} $data [9] = array (' ID ' => $id,
' Time ' => Date (' y-m-d h:i:s ', Time ())
);
}
if ($num <10) {$data [$num] = array (' ID ' => $id,
' Time ' => Date (' y-m-d h:i:s ', Time ())
);
}else {
return false;
}
}
}
Setcookie (' Recordluhuidudu ', Json_encode ($data), Time () +3600*24*30);
return true;
}