Understanding Cookies in PHP

前端 未结 4 534
鱼传尺愫
鱼传尺愫 2021-01-01 07:39

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

4条回答
  •  离开以前
    2021-01-01 07:53

    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

提交回复
热议问题