Jump to content

cookie only read after shutting down browser first

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?


Share this post

Link to post
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?

Share this post

Link to post
Share on other sites
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

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


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.

Share this post

Link to post
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) .




Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...