新创建的cookie不可用,直到随后的页面加载? [英] Are newly created cookies not available until a subsequent page load?

查看:98
本文介绍了新创建的cookie不可用,直到随后的页面加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次创建cookie我似乎并没有能够抓住这个Cookie,直到随后的页面加载。这是因为如果直到页面被请求的第二时间的cookie不存在到浏览器。

When I first create a cookie I don't seem to be able to grab that same cookie until a subsequent page load. It's as if the cookie doesn't exist to the browser until the page is requested a second time.

我使用的是Kohana的PHP框架:

I'm using the Kohana PHP framework:

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.

所以,我带领相信,这是正常的行为,而我也许并不了解Cookie的工作。任何人都可以澄清这对我?

So, I'm led to believe that this is normal behavior and that I probably don't understand how cookies work. Can anyone clarify this for me?

推荐答案

Cookie是在HTTP头设置,因此,当服务器返回的页面。
当你重新载入页面,你的浏览器将它们发送回服务器。

Cookies are set in HTTP headers, so when the server returns the page. When you reload the page, your browser will send them back to the server.

所以,这是完全正常的,他们只是一个新的请求后,看得见。

So, it is perfectly normal they are "visible" just after a new request.

下面是来自服务器的例子回应:

Here is an example response from the server:

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)

当你重新加载页面,你的浏览器发送的:

When you reload the page, your browser sends this:

GET / HTTP/1.1
Host: www.example.org
Cookie: name=value; name2=value2
Accept: */*

这就是为什么服务器只能通过后,浏览器中新的要求看他们。

This is why the server can see them only after a new request by the browser.

这篇关于新创建的cookie不可用,直到随后的页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆