简体   繁体   English

PHP Cookies不会设置

[英]PHP Cookies Won't Set

I'm on localhost. 我在本地主机上。 Much discussion has been done on cookies and sessions on localhost. 关于cookie和localhost上的会话的讨论很多。 However, my case appears to be exceptional. 但是,我的情况似乎很特殊。 I have tried setting cookies in different ways but none seems to work, even $_SESSION array remains empty after setting session variables. 我尝试以不同的方式设置Cookie,但似乎没有任何效果,即使设置会话变量后,即使$_SESSION数组也保持为空。 I have tried the following combinations to set cookie on my local host: 我尝试了以下组合以在本地主机上设置Cookie:

  • setcookie("name", "value")
  • setcookie("name", "value", 0)
  • setcookie("name", "value", 0, "/", false)
  • setcookie("name", "value", time()+86000, "/", false)
  • setcookie("name", "value", time()+86000)
  • setcookie("name", "value", time()+86000, ".localhost.com")
  • setcookie("name", "value", time()+86000, "localhost.com")
  • setcookie("name", "value", time()+86000, "localhost")
  • setcookie("name", "value", 0, "/", ".localhost.com")
  • setcookie("name", "value", 0, "/", ".localhost.com", false, false)
  • setcookie("name", "value", 0, "/", "localhost")
  • setcookie("name", "value", 0, "/", "localhost.com", false, false)
  • setcookie("name", "value", 0, "/", "localhost.com")

I also edited the session cookie values in PHP.ini file to reflect my futile trials with setcookie . 我还编辑了PHP.ini文件中的会话cookie值,以反映我对setcookie徒劳尝试。 I also tried changing the path from "/" to "/user" for all attempts. 我还尝试了所有尝试将路径从“ /”更改为“ / user”。

I'm using PHP 7.1 on Apache 2.4.33 and configured a "localhost.com" to point to a directory "/app_support" on the document root of my server. 我在Apache 2.4.33上使用PHP 7.1,并配置了“ localhost.com”以指向服务器文档根目录上的目录“ / app_support”。 I have also configured "app.localhost.com" to point to "/subdomains/app" directory, which is also on the root of the server. 我还配置了“ app.localhost.com”以指向“ / subdomains / app”目录,该目录也在服务器的根目录上。 I'm accessing my site through "app.localhost.com", then using AJAX to access "localhost.com/user". 我正在通过“ app.localhost.com”访问我的站点,然后使用AJAX访问“ localhost.com/user”。 On "localhost.com/user" directory, I have "user.php", a file with a class User that I use for logging in and setting the cookie. 在“ localhost.com/user”目录上,我有“ user.php”,这是一个带有User类的文件,我用于登录和设置cookie。

I have verified in Firefox 60 that the cookie headers are being sent for my custom cookies as well as for the session cookies, so it is clear that it is the browser that rejects them for some reason. 我已经在Firefox 60中验证过,正在为我的自定义Cookie和会话Cookie发送Cookie标头,因此很明显是由于某种原因浏览器拒绝了它们。 I get identical results in Chrome 68 and Chromium 66. 我在Chrome 68和Chromium 66中得到了相同的结果。

EDIT : Here's the parts where I'm setting the cookies. 编辑 :这是我设置cookie的部分。

session_regenerate_id();
$_SESSION['user']['id'] = $user['investor_id'];
$_SESSION['user']['surname'] = $user['surname'];
$_SESSION['user']['name'] = $user['given_name'];
$_SESSION['user']['email'] = $user['email'];
$selector = $this->generateCode(9);
$authenticator = $this->generateCode(33);
$expiry = time() + 2592000;

setcookie("logged_in", $selector.':'.$authenticator, $expiry, '/', '.localhost.com');

EDIT 2 : On Firefox, here are the cookie headers received: 编辑2 :在Firefox上,这是收到的Cookie标头:

logged_in   
    domain  .localhost.com
    expires 2018-09-15T08:35:07.000Z
    path    /
    value   F668B2928:417076134356498468FDA03D496336BDA
PHPSESSID   
    domain  localhost.com
    httpOnly    true
    path    /user
    value   77h432cjgu25mrnauktnc6s471

So, it turns out my problem was actually exceptional as it has nothing to do with PHP whatsoever. 因此,事实证明我的问题实际上是特殊的,因为它与PHP无关。 The problem with the cookies was because I was accessing the page setting the cookies via JQuery.ajax which causes the browser to automatically reject the cookies as discussed here due to CORS regulations. Cookie的问题是因为我正在通过JQuery.ajax访问设置cookie的页面由于CORS规定,这导致浏览器自动拒绝cookie,如此处所述。 My request is considered cross-domain since I'm sending a request from a subdomain. 由于我是从子域发送请求,因此我的请求被认为是跨域的。 The solution was not in the PHP, but on the client, as shown in this answer . 解决方案不是在PHP中,而是在客户端上, 如此答案所示。 However, if it is useful to you to have the 'x-requested-with' header retained by applying that solution, then you must manually set the header as shown in this answer . 但是,如果通过应用该解决方案保留'x-requested-with'标头对您有用,那么您必须手动设置标头, 如此答案所示。

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

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