Jump to content
BrainPill

transfer time cookie from server to browser

Recommended Posts

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 by BrainPill

Share this post


Link to post
Share on other sites

You need to go to another page OR refresh current page to see the new current cookie value, that is always been the case.

  • Like 1

Share this post


Link to post
Share on other sites

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.

  • Like 1

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