PHP cookie value question
Posted: Fri Mar 06, 2015 1:59 am
I have the following php codes and want to echo the cookie array item number.
<?php
$product_id = $item;
// if the cookie exists, read it and unserialize it. If not, create a blank array
if(array_key_exists('recentviews', $_COOKIE)) {
$cookie = $_COOKIE['recentviews'];
$cookie = unserialize($cookie);
} else {
$cookie = array();
}
// add the value to the array and serialize
$cookie[] = $product_id;
$cookie = serialize($cookie);
// save the cookie
setcookie('recentviews', $cookie, time()+3600);
?>
COOKIE: <?php echo $cookie; ?>
I can see the $cookie value.
However, I use use the method listed below but cannot get the answers I need.
<?php
$recent3 = $cookie[0];
$recent2 = $cookie[1];
$recent1 = $cookie[2];
?>
Please help me resolve this question, thank you in advance.
<?php
$product_id = $item;
// if the cookie exists, read it and unserialize it. If not, create a blank array
if(array_key_exists('recentviews', $_COOKIE)) {
$cookie = $_COOKIE['recentviews'];
$cookie = unserialize($cookie);
} else {
$cookie = array();
}
// add the value to the array and serialize
$cookie[] = $product_id;
$cookie = serialize($cookie);
// save the cookie
setcookie('recentviews', $cookie, time()+3600);
?>
COOKIE: <?php echo $cookie; ?>
I can see the $cookie value.
However, I use use the method listed below but cannot get the answers I need.
<?php
$recent3 = $cookie[0];
$recent2 = $cookie[1];
$recent1 = $cookie[2];
?>
Please help me resolve this question, thank you in advance.