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 cookie value question
Moderator: General Moderators
Re: PHP cookie value question
Where are you putting that code? In the first part you reuse $cookie and it can be either the serialized string or an array.
And you should know: what you're doing is not safe. The user can edit their own cookies and that means they can trick your code into unserialize()ing bad data that can cause all sorts of problems for you.
Use the session, not a cookie.
And you should know: what you're doing is not safe. The user can edit their own cookies and that means they can trick your code into unserialize()ing bad data that can cause all sorts of problems for you.
Use the session, not a cookie.