Jump to content
BrainPill

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 cookie value once set is only accessible once you go to another page OR reload the current page it was set in. They have always worked that 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
<?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.

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

Trouble is I DON'T know how your code is laid out? so I can't possibly give you an answer what? how,? why? it does this to fix it.

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.

Guest
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.

Loading...

×
×
  • Create New...