未设置会话Cookie [英] Session cookie not being set

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

问题描述

无法在本地计算机上运行php的$ _SESSION

can't get php's $_SESSION to work on my local machine

它可以在实时服务器上正常运行,但不能在本地计算机上运行

it's working just fine on a live server but it's not working on a local machine

我尝试在Windows上使用apache和php,在mac上使用apache和php,在debian上使用apache和php,它们都不起作用(我的实时服务器也正在运行debian 9,我在本地尝试过同样的方法)

i have tried with apache and php on windows, apache and php on mac and apache and php on debian, none of them work ( my live server is also running debian 9, the same one i tried locally )

我可以看到php正在发送set-cookie,但是由于某些原因它没有被设置(存储中没有cookie> cookie,并且我的脚本无法正常工作设置了Cookie)

in firefox' developer tools > network > headers i can see that php is sending the set-cookie but for some reason it's not being set ( no cookies in storage > cookies, and my script isn't working as it should when cookie is set )

我未使用ssl/https,并且我设置了"session.cookie_secure = 0并关闭",但是由于某种原因,这就是set-cookie标头的样子:"Set-Cookie:PHPSESSID = XXXXX; path =/; HttpOnly; Secure",我不认为php应该设置Secure标志,因为我在php.ini中明确禁用了它?

i'm not using ssl/https and i have set "session.cookie_secure = 0 and off" but for some reason this is what the set-cookie header looks like: "Set-Cookie: PHPSESSID=XXXXX;path=/;HttpOnly;Secure", i don't think php should be setting the Secure flag since i explicitly disabled it in php.ini?

是的,每个使用会话功能的文件中都有一个session_start()

and yes, every file that uses session functionality has a session_start() in it

没有任何apache/php错误,我什至启用了xdebug

there are no apache/php errors whatsoever, i even have xdebug enabled

使用localhost,127.0.0.1、10.0.0.10(my lan ip)和自定义主机名进行了测试,没有任何作用

tested using localhost, 127.0.0.1, 10.0.0.10 ( my lan ip ), and custom hostname, none work

我没主意了,尝试了我能想到的一切

i'm out of ideas, tried everything i could think of

在具有php 7.2和默认配置的实时debian 9服务器上工作

works on a live debian 9 server with php 7.2 and default configuration

在使用php 7.2和默认配置的本地debian 9服务器上不起作用

doesn't work on a local debian 9 server with php 7.2 and default configuration

在具有相同apache/php版本的Windows上不起作用

doesn't work on windows with the same apache/php versions

在Mac上无法与Apache 2.4和php 7.3一起使用,甚至不能与session.cookie_secure = 0设置

doesn't work on mac with apache 2.4 and php 7.3, not even with session.cookie_secure=0 set

检查了apache和php错误,没有错误

checked for both apache and php errors, there are none

使用firefox的开发人员工具查看标题/cookies

used firefox' developer tools to see headers/cookies

检查了我的代码,并确保它具有session_start()并且其他所有内容都是正确的

checked my code and made sure it has session_start() and everything else is correct

我什至尝试将安全标志设置为false手动设置cookie,然后再次在Set-Cookie标头中设置安全",这是代码:

i even tried manually setting the cookie with the secure flag set to false and again "Secure" is being set in Set-Cookie header, this was the code:

setcookie("PHPSESSID", "7nhqdim7uu2viae7vhhf9os5ue", 0, "/", "", false, false);

这是我用于测试的代码:

and here is the code i use for testing:

<?php
session_start();

var_dump($_SESSION);

if(isset($_POST['submit']))
{
  $_SESSION['value'] = $_POST['example'];
  header('Location: /session.php'); // session.php is this file
}

if(isset($_SESSION['value']) && $_SESSION['value'] == 'example')
{
  echo "value is " . $_SESSION['value'] . '<br>';
}
?>
<form method="post">
  <input type="text" name="example" value="example">
  <input type="submit" name="submit" value="submit">
</form>

推荐答案

会话cookie需要第二级域,您不能为诸如com之类的顶级域(TLD)设置cookie,因为这将是一种安全性问题.意味着任何带有 .com 的网站都将允许该cookie,但这并不是一件好事.为 localhost 设置cookie就像为 com net org

Session cookie requires a second-level domain, you cannot set a cookie for a top-level domain (TLD) such as a com because that would be a security issue. Meaning any site with a .com would allow that cookie and that would not be a good thing. Setting a cookie for localhost is like setting a cookie for a com or net or org

要使其正常工作,您将需要将Cookie设置为 localhost.com 之类的内容,例如:

To get it to work you will need to set your cookie for something like localhost.com for example:

session_set_cookie_params(0, '/', 'localhost.com');

在主机文件中添加一个条目:

Add an entry to your hosts file:

127.0.0.1   localhost.com

在MacOS上:

sudo vi /private/etc/hosts

在Windows上编辑此文件:

On Windows edit this file:

C:\Windows\System32\Drivers\etc\hosts

在Linux上:

sudo vi /etc/hosts

最后在例如 localhost.com 上运行您的应用程序(不使用Apache):

Finally run your app on localhost.com, for example (without Apache):

php -S localhost.com -t

然后使用.com在浏览器中打开:

And then open in the browser also using .com:

http://localhost.com/yourapp

希望这会有所帮助.

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

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