Phpcms Commodity browsing record and the problems encountered

Source: Internet
Author: User
Tags setcookie

Using cookies

function _sethistory ($data)
{
if (! $data | |!is_array ($DATA))
{
return false;
}
Determine if there is a browsing record in the cookie class
if ($_cookie["_producthistory"])
{
$history = Unserialize (stripslashes ($_cookie[' _producthistory '));

Array_unshift ($history, $data); At the top of the browsing record, add
/* Remove duplicate records */
$rows = Array ();
foreach ($history as $v)
{
if (In_array ($v, $rows))
{
Continue
}
$rows [] = $v;
}
/* If the number of records is 5 extra, remove */
while (count ($rows) > 6)
{
Array_pop ($rows); Pop
}
Setcookie (' _producthistory ', serialize ($rows), time () + 3600 * 24 * 30, '/');
}
Else
{
$history = Array ($data);
Setcookie (' _producthistory ', $history, time () + 3600 * 24 * 30, '/');
}

return;
}

I was after a long time to fix, start in window no problem, to the Linux environment can not be deserialized, and finally found that there are special characters caused.

Note:
Serialize () – Converts an array to a string and keeps the data structure intact
Addslashes () – The function adds a backslash before the specified predefined character. These predefined characters are:
Single quotation mark (')
Double quotation marks (")
Back slash (\)
Null
The Stripslashes () function is a reverse operation of addslashes (), which is to delete the backslash added by the addslashes () function.
Unserialize () operates on a single serialized variable and converts it back to the value of PHP. Returns the converted value, which can be an integer, float, string, array, or object. Returns FALSE if the passed string is not serializable.

$_cookie[' Dc_orders ']=serialize ($_session[' dingcan_shoppingcart_data ']);

$cookie _info=unserialize (stripslashes ($_cookie[' dc_orders '));

Phpcms Commodity browsing record and the problems encountered

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.