BrainPill Posted May 10, 2019 Share Posted May 10, 2019 (edited) hello, I have an issue with getting the $_COOKIE value of a just set cookie. In the example the cookie is set with header. Immediately after having it set, I want to fetch the value of the browser cookie with $_COOKIE, but then the cookie is not there yet. Executing the script again shows the old cookie value and the new value is dumped on screen. I have the idea it takes time before the cookie is placed in the browser. questions: How can I check the time it takes before the cookie (headers?) are in the browser? And how can you 'wait' before the $_COOKIE part is executed ? <?php // test example $dt_tm = gmdate('r',strtotime('+2 hours')); session_start(); $sid = session_id(); var_dump($sid); $cknm = 'your_cookie_name'; header("Set-Cookie: $cknm=$sid; expires=$dt_tm; path=/testpath/; domain= www.yourvirtualhost.localhost; HttpOnly; SameSite=strict"); var_dump($_COOKIE); echo $_COOKIE['your_cookie_name']; ?> Edited May 10, 2019 by BrainPill Link to comment Share on other sites More sharing options...
dsonesuk Posted May 10, 2019 Share Posted May 10, 2019 You need to go to another page OR refresh current page to see the new current cookie value, that is always been the case. 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted May 10, 2019 Share Posted May 10, 2019 It's not an issue of time. Cookies are sent as HTTP headers with either the request or the response. When your browser sends the HTTP request to the server, it includes any cookies that match the request as headers. You can use your browser's developer tools to see that. When the server is sending the response back, it will also send headers. If your PHP code sets a cookie, then the response headers will include a Set-Cookie header to tell the browser to set a new cookie. So when you set a cookie on one response you can't check the same cookie in that request. That request was sent before the browser was told to set the cookie. The cookie will be sent in the next request. 1 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