首页 > 解决方案 > PHP Cookie 未分配给全局数组

问题描述

我正在设置一个带有哈希值的 cookie,用于从数据库中引用会话变量。我在启动会话时使用 setcookie。稍后通过 $_COOKIE 引用哈希。这适用于我的本地机器。但是,当我把它放在服务器上时,$_COOKIE 数组是空的。令人困惑的部分是 cookie 在请求标头中。我在 php.ini 中variables_order = "GPCS"都试过了。variables_order = "EGPCS"我在 setcookie 的参数上尝试了无数种变化。我已经从等式中删除了我的负载均衡器(VPN,将主机文件直接指向 web1 服务器)。我不知道为什么 $_COOKIE 数组是空的,并且即将直接从标题中读取。我已经尝试过nginx和apache。配置在 repo 中。dev 和 prod 之间的唯一区别是 server_name。

设置 Cookie:

private function setSessionCookie($value, $timeoutHours) {
    $timeout = time() + (60 * 60 * ($timeoutHours));
    setcookie(__SESSION_KEY, $value, $timeout, "/");//, $this->configuration["domain"]);
}

获取 Cookie:

if(isset($_COOKIE[__SESSION_KEY])) { ... }

(getallheaders()) 的 var_dump

/var/www/sites/test/public/index.php:29:
array (size=13)
  'Cookie' => string 'session=c2573edd25a0f7d6bb75448f568cf892e09cae3a85672d6751c6f735a9b19d37' (length=85)
  'Accept-Language' => string 'en-US,en;q=0.9' (length=14)
  'Accept-Encoding' => string 'gzip, deflate' (length=13)
  'Referer' => string 'test.local/signin' (length=35)
  'Accept' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' (length=135)
  'User-Agent' => string 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36' (length=104)
  'Upgrade-Insecure-Requests' => string '1' (length=1)
  'Cache-Control' => string 'no-cache' (length=8)
  'Pragma' => string 'no-cache' (length=8)
  'Connection' => string 'keep-alive' (length=10)
  'Host' => string 'test.local' (length=21)
  'Content-Length' => string '' (length=0)
  'Content-Type' => string '' (length=0)

$_COOKIE 的 var_dump

/var/www/sites/test/public/index.php:30:
array (size=0)
  empty
111

标签: phpcookiesheader

解决方案


推荐阅读