eys*_*kal 2 php browser cookies kohana
当我第一次创建cookie时,我似乎无法在后续页面加载之前获取相同的cookie.就好像cookie在浏览器中不存在,直到第二次请求页面为止.
我正在使用Kohana PHP框架:
Cookie::set('new_cookie', 'I am a cookie');
$cookie = Cookie::get('new_cookie');
//$cookie is NULL the first time this code is run. If I hit the page again
and then call Cookie:get('new_cookie'), the cookie's value is read just fine.
Run Code Online (Sandbox Code Playgroud)
所以,我被认为这是正常的行为,我可能不明白cookie是如何工作的.谁能为我澄清一下这个?
Cookie在HTTP标头中设置,因此当服务器返回页面时.当您重新加载页面时,您的浏览器会将它们发送回服务器.
因此,在新请求之后,它们"可见"是完全正常的.
以下是来自服务器的示例响应:
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
Set-Cookie: name2=value2; Expires=Wed, 09-Jun-2021 10:18:14 GMT
(content of page)
Run Code Online (Sandbox Code Playgroud)
当您重新加载页面时,您的浏览器会发送以下内容:
GET / HTTP/1.1
Host: www.example.org
Cookie: name=value; name2=value2
Accept: */*
Run Code Online (Sandbox Code Playgroud)
这就是服务器只有在浏览器发出新请求后才能看到它们的原因.