来自另一个页面的会话变量在子域上不可见

session variable from another page is not visible on a subdomain

login.php

session_start();
$_SESSION['aid'] = 1;
echo $_SESSION['aid']; // 1

然后index.php

session_start();
echo $_SESSION['aid']; // nothing is echoed  
$_SESSION['test'] = 323;
echo $_SESSION['test']; // 323

error_log

PHP Notice: Undefined index: aid in...

这只发生在我的子域上 - admin.example.com
在主域上 - example.com - 一切正常
这也只发生在远程服务器上
在我的本地主机上 - xampp, win7, chrome - 没关系

有什么帮助吗?

您应该在开始会话之前将 cookie 域设置为“.example.com”(在所有子域的所有脚本上, 否则 cookie 在不同的(子)域上什至无效。

session.cookie_domain = ".example.com"

或使用session_set_cookie_params():

session_set_cookie_params(0, '/', '.example.com');
session_start(); 

另见这个问题: Allow php sessions to carry over to subdomains