php设置cookie的问题

发布于 2024-11-05 21:53:46 字数 382 浏览 5 评论 0原文

我使用以下语法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-11-12 21:53:47

正如我在评论中所说,根据您的描述(虽然相当模糊且不太容易理解),我认为问题可能是您在将 cookie 发送到服务器之前尝试读取它。

cookie 的工作方式如下:

  1. 您发出请求
  2. 服务器将 cookie 标头发送回客户
  3. 端 页面加载 - Cookie 在该页面加载上对 PHP 可见
  4. 刷新
  5. 客户端将 cookie 标头发送至服务器
  6. 服务器接收 cookie标头,因此 PHP 可以读取它
  7. 页面加载 - Cookie IS 在此可见。

如果您还没有尝试过,请再次刷新!

由于您想在设置的同时读取它,因此只需存储您正在设置的值并使用它即可。或者(尽管未经测试),您可以在 $_COOKIE 数组中手动​​设置它。

所以像这样:

setcookie("helloworld", .. );
$_COOKIE['helloworld'] = $value;

然后你就可以正常阅读了。请注意,我真的不建议覆盖自动超全局的值(同样适用于 $_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:

  1. You make a request
  2. Server SENDS cookie header back to client
  3. Page loads - Cookie is NOT visible to PHP on this page load
  4. Refresh
  5. Client SENDS cookie header to server
  6. Server RECEIVES cookie header thus PHP can read it
  7. Page loads - Cookie IS visible here.

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:

setcookie("helloworld", .. );
$_COOKIE['helloworld'] = $value;

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 to account.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 to account.php.

This way, account.php can always see your cookie.

不念旧人 2024-11-12 21:53:47

它可能相关或不相关,但您正在将布尔值分配给需要整数或字符串的参数。如果您是 PHP 新手,仔细阅读手册并理解函数签名非常重要。在这种情况下,您必须检查 http://php.net/setcookie ,您可以在其中阅读以下内容:

bool setcookie ( 字符串 $name [,
字符串 $value [, int $expire = 0 [,
字符串 $path [,字符串 $domain [,布尔
$secure = false [, bool $httponly =
假]]]]]])

此外,当我运行您的代码时,我收到警告:

Warning: Cookie names cannot contain any of the following '=,; \t\r\n\013\014'

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:

bool setcookie ( string $name [,
string $value [, int $expire = 0 [,
string $path [, string $domain [, bool
$secure = false [, bool $httponly =
false ]]]]]] )

Also, I'm getting a warning when I run your code:

Warning: Cookie names cannot contain any of the following '=,; \t\r\n\013\014'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文