I'm using the code:
IF (isset($_GET['s']))
{
$sessie = $_GET['s'];
}
ELSE
{
$sessie = 'I';
}
$cookie = 'JaiDje_Page';
setcookie($cookie, $sessie);
$WDpag = $_COOKIE[$cookie];
echo $WDpag."-".$_COOKIE[$cookie]."-".$sessie."<br>";
After a new value for $sessie (menu choise) the strange thing is that the first time the page is loaded, $WDpag and $_COOKIE[$cookie] are giving the old value and $sessie is giving the new value. After a page refresh all three values are the same.
So after making a choise in the menu the following is echos (exampel)
I-I-Z
than after a page refresh (F5)
Z-Z-Z
It seems that the cookie is one step behind the choise in my menu.
What am I doing wrong?
$_COOKIE
is set when the page loads so If you want immediate access, you can set $_COOKIE['variable']
direct.
so do like this
//setcookie($cookie, $sessie);
$_COOKIE[$cookie] = $sessie;
The reason is that $_COOKIE[$cookie]
returns the current cookie sent by the browser. While setcookie
sends a cookie to the browser. When you retrieve the value $_COOKIE[$cookie]
, the cookie is not yet set because it will be set after the page is requested.
try to put this after setcookie
:
if($_COOKIE[$cookie] != $sessie) header("location: pageName.php");