php设置cookie问题 [英] php set cookie issue

查看:50
本文介绍了php设置cookie问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下语法在本地主机上设置 cookie

I'm setting a cookie on localhost with the following syntax

setcookie("testCookie ", "hello cookie", false, "/", false);

问题是我第一次访问页面时创建了 cookie 并显示了萤火虫

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

如何在第一次加载页面时获取要读取的cookie的值?

How can I get the value of the cookie to be read the first time the page is loaded?

推荐答案

正如我在评论中所说的那样,根据您的描述(虽然相当模糊且不太好理解),我认为问题可能在于您正在尝试阅读发送到服务器之前的 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.

cookie 的工作方式如下:

The way a cookie works is as follows:

  1. 您提出请求
  2. 服务器将 cookie 标头发送回客户端
  3. 页面加载 - Cookie 在此页面加载时对 PHP 可见
  4. 刷新
  5. 客户端向服务器发送 cookie 标头
  6. 服务器接收 cookie 标头,因此 PHP 可以读取它
  7. 页面加载 - Cookie 在此处可见.
  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!

因为您想在设置它的同时读取它,所以只需存储您设置的值并使用它.或者(尽管这是未经测试的),您可以在 $_COOKIE 数组中手动​​设置它.

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.

就像这样:

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

然后就可以正常阅读了.请注意,我真的不建议覆盖自动超全局变量的值(同样适用于 $_REQUEST$_POST$_GET 等.),而是建议您只存储您正在设置的值并使用它.

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.

另一种方法是使用一种网关"形式,这意味着您将在网关页面上设置 cookie,然后该页面将继续将您重定向到下一页.

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.

例如,假设您的流程如下:login.php -> account.php.您有 2 个选项,而不是直接将登录表单发布到 account.php.

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.

选项 1:POST 回 login.php,设置 cookie,然后重定向到 account.php.选项 2:有一个网关,例如 logincheck.php,POST 到那个,设置 cookie,然后重定向到 account.php.

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.

这样,account.php 就可以随时看到您的 cookie.

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

这篇关于php设置cookie问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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