I have this code in a php page:
if(!isset($_COOKIE[$cookie_name])) {
if(!setcookie($cookie_name, mt_rand(), time() + (86400 * 30), "/")) {
die("[ERROR COOKIE] Failed to set cookie!");
}
}
If I don't have any cookie set in my browser the first time the page is loaded $_COOKIE[$cookie_name]
is set to 0
. Afterwards, if I reload the page, it is set to a random number as expected.
So far I just make a workaround like this:
if(!isset($_COOKIE[$cookie_name])) {
if(!setcookie($cookie_name, mt_rand(), time() + (86400 * 30), "/")) {
die("[ERROR COOKIE] Failed to set cookie!");
}
if($_COOKIE[$cookie_name] == 0) {
echo "<meta http-equiv=\"refresh\" content=\"0; url=./\" />";
die();
}
}
but what I'd like to understand is why the first time setcookie() defaults to 0
.