Jump to content

cookie only read after shutting down browser first


BrainPill

Recommended Posts

I am trying to set a cookie that expires in 20 minutes.

The cookie is set i can see it in google chrome.

but when I try to read the values with var_dump($_COOKIE) the specific cookie is not shown.

Only if I shut down the browser first the cookie is readable for the php script. 

How can I make this readable instantly? Should I change header settings and in what way?

 

Link to comment
Share on other sites

 

The situation is that the user 

1) fills in a form and the cookie is set

2) an email is sent to the user

3) user clicks to an activation page. 

 

what do you mean with reload, is there a possibility to reload with PHP or do you mean it can be done with headers?

Link to comment
Share on other sites

<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
    <body>

	<?php
	if (!isset($_COOKIE[$cookie_name])) {
	    echo "Cookie named '" . $cookie_name . "' is not set!";
	    //header("Refresh:0");
	    //header('Location: http://localhost/cookietest.php');
	} else {
	    echo "Cookie '" . $cookie_name . "' is set!<br>";
	    echo "Value is: " . $_COOKIE[$cookie_name];
	}
	var_dump($_COOKIE);
	?>

    </body>
</html>

The cookie is set on initial page load, but not yet available to gather data from that cookie until reload takes place.

Clear all cookie storage and run above code:  var_dump does not show set cookie data, manually click browser reload and the cookie data is shown.

Clear all cookie storage and run above code and uncomment one of the header coding, IF cookie is data is not available either header will cause a reload of current page and the data will show in the var_dump() without manual reload.

Link to comment
Share on other sites

Not sure if the problem is a php problem.

Most of the times my cookie settings work well but it seems to happen arbitrary that a cookie is read out by php and not read out.

If I check 2 different settings with the link click procedure (explained in my last posting) and I do this 2 times then the first one works and instantly repeating the procedure skips the condition , the $_COOKIE dump does not show all cookies (does it the first time) .

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...