this my code but i am not getting the my cookie value in page
if (isset($_COOKIE['mycookie'])) {
$cookie[] = $proID;
$cookie = implode(',', $cookie);
$cookie = unserialize($cookie);
setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/');
}
You are doing isset($_COOKIE['mycookie'])
which will check if the $_COOKIE['mycookie']
is set or not, will return true
if set else false
. And if you are setting it inside the if
block for the first time it will never be set. So it should be -
if(!isset($_COOKIE['mycookie']))
// Set the cookie
}
First get the old value of the cookie, and turn it into an array with unserialize
. If the cookie isn't set, initialize it to an empty array.
if (isset($_COOKIE['mycookie'])) {
$cookie = unserialize($_COOKIE['mycookie']);
} else {
$cookie = array();
}
Then add the new element to the array, and save it back into the cookie.
$cookie[] = $proID;
setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/');