When the following code is run for the first time, it does not echo out the \"var1\" value but it does on browser refresh (f5), Why? As i understand when the browser sends t
I believe this has to do with the way $_COOKIE is populated. According to the reference material, it comes from any cookies sent by the client. The cookie is sent in the HTTP header when you make a request for a page, so it makes sense that it only works the second time. The first time around, you are setting the cookie (client doesn't know about it yet to send it along with the first request). When you refresh, the cookie has been set by the first page load, and the client thus sends the cookie with the request, which PHP sees and populates into the $_COOKIE variable.
http://php.net/manual/en/features.cookies.php
When calling setcookie
, PHP sets an HTTP Cookie
header that's delivered to the client together with the rest of the output of the script. The client (browser) will then store this cookie in its cookie storage and send it back to the server on subsequent requests.
$_COOKIE
contains all the cookies that have been received in the current request from the client. The first time that's nothing, since the client hasn't received the Cookie
header yet. Only on subsequent requests does the client send the cookie which gets stored in $_COOKIE
. setcookie()
does not populate the $_COOKIE
variable.
Client Server
1. --> $_COOKIE is empty
2. Cookie
store <-- setcookie()
3. Cookie
send --> $_COOKIE is set
It's all explained in the manual. setcookie() causes a Set-Cookie:
response header to be returned to the browser. The $_COOKIE array can only be filled with the next HTTP refresh when the browser had the opportunity to reply back with the Cookie:
request header.
When the following code is run for the first time, it does not echo out the "var1" value but it does on browser refresh (f5), Why?
The browser needs to send the cookie back. On the first request it doesn't know about that cookie yet. After the refresh it does. Only then it can send it.
As i understand when the browser sends the code to the server,
On the second request.
setcookie() stores the cookie variable ("var1") to a client (browser) in a local file
The browser saves it.
and puts the "var1" value available in global domain via $_COOKIE superglobal.
Not immediately.
Since "var1" value is available immediately in $_COOKIE after the first server replies to browser's initial request, then why the "var1" is not echoed out.
It is not immediately in $_COOKIE. It can't be. That array is only populated once, when PHP starts.
Is it that setcookie() stores "var1" value in client's browser on first request and only when the page is refreshed (2nd request) the browser sends back "var1" value to the server and then the server makes it available in the global domain via $_COOKIE function.
Yes. That's how it works.
Cookies are stored on response and loaded from user on request, like a get header or a post header.
Your http response does not trigger an http request.(Which is perfectly normal)
setcookie()
Sets a cookie on the client by sending a cookie header, not on the server. the $_COOKIE is set by the cookie header coming from an http request.