I am using the function set cookie to set (id) cookie like so:
$cookie_id = $insert_userid;
setcookie("id", $cookie_id, time() + 31556926, '/', 'www.example.com');
The code above is working fine. I am checking the setting with firefox and the correct cookie is there.
Now on my other pages on my webiste I want to check if the cookie is set.
I am using this code to redirect if the cookie is not set.
I am getting redirected in both ways weather the cookie is set or not.
<?php
if(isset($_COOKIE['id'])){
//do something.
}else{
header('Location: login.php');
}
?>
how do I fix this problem ?
Var dump result is :
array(4) { ["link"]=> string(43) "http://www.zzz.com/zzz" ["__unam"]=> string(30) "a5caeed-1385397a335-7fca86ad-2" ["PHPSESSID"]=> string(26) "6khzzzzzzzzjd51i6hamm0" ["id"]=> string(10) "zzzzzzzzzz" }
Update: I am not checking the cookie from the same page I crated it.
To help find the problem, try this:
Put that before the
if. Ideally, you should see the cookie’s value. If you seeNULL, then the cookie is not being passed.If it’s not being passed, try just
var_dump($_COOKIE); exit;and see if you get an array and, if so, if there is anidkey.Make sure that the cookie is being sent, check with Developer Tools or whatever your browser’s equivalent is.