PHP Cookies Record User history browsing information examples

Source: Internet
Author: User
Tags setcookie


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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.