php设置cookie的问题
我使用以下语法在 localhost 上设置 cookie
setcookie("testCookie ", "hello cookie", false, "/", false);
问题是我第一次访问创建 cookie 的页面并且 firebug 显示
Cookie testCookie added. hello cookie
但它不读取该值。如果我刷新页面,该值将被读取,并且 fire bug 显示
Cookie testCookie changed. hello cookie
如何在第一次加载页面时获取要读取的 cookie 的值?
I'm setting a cookie on localhost with the following syntax
setcookie("testCookie ", "hello cookie", false, "/", false);
The problem is the first time I visit the page the cookie is created and firebug shows
Cookie testCookie added. hello cookie
But it does not read the value. If I refresh the page, the value is read and fire bug shows
Cookie testCookie changed. hello cookie
How can I get the value of the cookie to be read the first time the page is loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我在评论中所说,根据您的描述(虽然相当模糊且不太容易理解),我认为问题可能是您在将 cookie 发送到服务器之前尝试读取它。
cookie 的工作方式如下:
如果您还没有尝试过,请再次刷新!
由于您想在设置的同时读取它,因此只需存储您正在设置的值并使用它即可。或者(尽管未经测试),您可以在
$_COOKIE
数组中手动设置它。所以像这样:
然后你就可以正常阅读了。请注意,我真的不建议覆盖自动超全局的值(同样适用于
$_REQUEST
、$_POST
、$_GET
等.),并建议您只存储您正在设置的值并使用它。另一种方法是使用“网关”形式,这意味着您可以在网关页面上设置 cookie,然后该页面将继续将您重定向到下一页。
例如,假设您的流程如下:
login.php
->account.php
。您有 2 个选择,而不是直接将登录表单 POST 到account.php
。选择 1:POST 返回到 login.php,设置 cookie,然后重定向到
account.php
。选择 2:拥有一个网关,例如
logincheck.php
,通过 POST 到该网关,设置 cookie,然后重定向到account.php
。这样,
account.php
始终可以看到您的 cookie。As I put in my comment, from your description (although fairly vague and not too understandable), I think the issue may be that you're trying to read the cookie before it's sent to the server.
The way a cookie works is as follows:
If you haven't tried already, refresh again!
Since you want to read it at the same time you're setting it, just store the value you're setting and use that. Alternatively (although this is untested), you could manually set it in the
$_COOKIE
array.So something like this:
Then you can read it normally. Note that I wouldn't really recommend overriding the value of an automatic superglobal (same goes for
$_REQUEST
,$_POST
,$_GET
, etc.), and would instead suggest that you just store the value you're setting and use that.Another approach would be to use a form of "gateway", meaning you'd set the cookie on a gateway page, which will then continue to redirect you to the next page.
For example, say your flow was as follows:
login.php
->account.php
. Rather than POST'ing your login form straight toaccount.php
you have 2 options.Opt 1: POST back to login.php, set the cookie, and then redirect to
account.php
.Opt 2: Have a gateway, such as
logincheck.php
, POST through to that, set the cookie, and then redirect toaccount.php
.This way,
account.php
can always see your cookie.它可能相关或不相关,但您正在将布尔值分配给需要整数或字符串的参数。如果您是 PHP 新手,仔细阅读手册并理解函数签名非常重要。在这种情况下,您必须检查 http://php.net/setcookie ,您可以在其中阅读以下内容:
此外,当我运行您的代码时,我收到警告:
It may be related or not, but you are assigning boolean values to parameters that expect integers or strings. If you are new to PHP, it's very important that you read the manual carefully and understand function signatures. In this case you have to check http://php.net/setcookie where you can read this:
Also, I'm getting a warning when I run your code: