BrainPill Posted October 20, 2019 Share Posted October 20, 2019 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 More sharing options...
dsonesuk Posted October 20, 2019 Share Posted October 20, 2019 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. Link to comment Share on other sites More sharing options...
BrainPill Posted October 21, 2019 Author Share Posted October 21, 2019 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 More sharing options...
dsonesuk Posted October 21, 2019 Share Posted October 21, 2019 <?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 More sharing options...
BrainPill Posted October 22, 2019 Author Share Posted October 22, 2019 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 More sharing options...
dsonesuk Posted October 22, 2019 Share Posted October 22, 2019 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. Link to comment Share on other sites More sharing options...
BrainPill Posted October 27, 2019 Author Share Posted October 27, 2019 sorry I think its too much to place all my code here. I hope I can make a test simulation maybe in the future. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now