php - 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
解决方案
推荐阅读
- php - 使用 W5100 shield 向 Web 服务器发送数据时出现问题
- python-3.x - Scipy vs Matlab:3D 旋转矩阵
- r - 如何将 output$suffix 名称的后缀合并到 R shiny 中的 input$suffix_rows_selected 函数中?
- r - 将反应输出存储在向量中 - Shiny R
- azure - Azure 应用服务未记录所有 App Insights 依赖项
- amazon-web-services - 为 CodeDeploy 使用最新的 ECS 任务修订
- linux - 无法在并行脚本中使用位置参数
- c# - 无法确定 JSON 对象类型系统参数异常
- c# - 如何使用 c# 将 TapGestureRecognizer 添加到从 JSON 创建的项目中?
- ruby-on-rails - Ruby on Rails 应用程序中的 sitemap.xml 问题