简体   繁体   English

Cookie存在于浏览器中,但是php $ _COOKIE为空

[英]Cookie is present in browser, but php $_COOKIE is empty

I have a project that works on a local server but not on my production server, due to cookies not being seen by the server. 我有一个项目可以在本地服务器上运行,但不能在我的生产服务器上运行,这是因为服务器无法看到Cookie。 I've made a minimal version of the code that reproduces the issue on that server: 我制作了代码的最低版本,该代码在该服务器上重现了该问题:

<?php

if(!isset($_COOKIE['foo'])){
    setcookie('foo', 'bar', time() + 7*24*60*60, '/');
    echo "Cookie was not found, so we just created it.";
} else {
    echo "Cookie was found!";
}

?>

No matter how many times I refresh this page, I always get the "not found" message. 无论我刷新此页面多少次,我总是会收到“未找到”消息。 Whenever I try to log the $_COOKIE variable, I get an empty Array. 每当我尝试记录$_COOKIE变量时,都会得到一个空数组。 However: 然而:

  • The cookie is present in the browser, and correctly sent with the request cookie存在于浏览器中,并与请求一起正确发送
  • The cookie is set and read in the same file (it's not an issue with the path ) 设置并在同一文件中读取Cookie( path不是问题)
  • There is no output before setcookie , and the file is encoded in UTF8 without BOM setcookie之前没有输出,并且文件使用UTF8编码而没有BOM

I think this is a server configuration issue, since the code works locally, but I have no idea where to look. 我认为这是服务器配置问题,因为代码在本地运行,但是我不知道在哪里查看。 Has anyone seen this before, do you know what could cause this? 有没有人看过这个,你知道是什么原因造成的吗?

If you need more info, just tell me and I'll add it to my question. 如果您需要更多信息,请告诉我,我将其添加到我的问题中。 Thank you! 谢谢!

If there's a cache server or CDN involved, it may be filtering cookies based on a whitelist. 如果涉及缓存服务器或CDN,则可能是根据白名单过滤cookie。 This is to improve caching, since each request with a unique set of cookies would need to be regarded as different from other requests and could not be cached (you may receive a different reply from the server based on your cookies, so the cache server cannot serve you the cached response of a previous client). 这是为了改善缓存,因为每个具有唯一Cookie集合的请求都将被视为与其他请求不同,因此无法缓存(您可能会基于Cookie收到来自服务器的不同回复,因此缓存服务器无法为您提供前一个客户端的缓存响应)。 Since lots of services are setting cookies which may be sent to the server (eg analytics packages) but have absolutely no influence on the contents of the response, heeding all cookies by default would often completely subvert caching. 由于许多服务都设置了可以发送到服务器的cookie(例如,分析程序包),但对响应的内容绝对没有影响,因此默认情况下注意所有cookie通常会完全破坏缓存。

Configure the caching server in charge to specifically pay attention to your cookie and to forward it to the origin server. 配置负责的缓存服务器以特别注意您的cookie并将其转发到原始服务器。 If you have a lot of different cookies, consider giving them a common prefix and whitelist that (eg foo-* ). 如果您有很多不同的cookie,请考虑给它们提供通用的前缀和白名单(例如foo-* )。

I just dealt with the same issue, my production server was allowing me to create a browser cookie using setcookie('cookieNameHere', $cookieValueHere, time() + (86400 * 365), "/") , but the $_COOKIE variable was always an empty array. 我只是处理了同样的问题,我的生产服务器允许我使用setcookie('cookieNameHere', $cookieValueHere, time() + (86400 * 365), "/")创建浏览器cookie,但是$_COOKIE变量是总是一个空数组。

The issue was that my production server blocked direct access to the $_COOKIE variable contents via PHP for security reasons. 问题是出于安全原因,我的生产服务器禁止通过PHP直接访问$_COOKIE变量内容。

My solution was to access the cookie value via JavaScript using the following function: 我的解决方案是使用以下功能通过JavaScript访问Cookie值:

    function getCookie(cname) {
      var name = cname + "=";
      var decodedCookie = decodeURIComponent(document.cookie);
      var ca = decodedCookie.split(';');
      for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
          c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length);
        }
      }
      return "";
    }

I continued to create/update the cookie via PHP. 我继续通过PHP创建/更新cookie。

FYI, I was working on a WordPress site hosted on WP-Engine. 仅供参考,我正在WP-Engine托管的WordPress网站上工作。 See this page for an in depth explanation, and for other options in the event you absolutely need to access a cookie value via PHP (ADMIN-AJAX calls, etc). 请参阅此页面以获取详细说明,以及在绝对需要通过PHP(ADMIN-AJAX调用等)访问cookie值的情况下的其他选项。

I've had similar problem, and it turned out to be HAProxy configuration issue. 我遇到过类似的问题,事实证明这是HAProxy配置问题。 Do you have any load balancer between the server and the user? 服务器和用户之间是否有任何负载平衡器?

I think your problem is the server time. 我认为您的问题是服务器时间。 Check your time with time() function and compare with local time. 使用time()函数检查您的时间,并将其与本地时间进行比较。 Maybe one of them is wrong. 也许其中之一是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM