我有一个运行在Azure上的PHP7应用程序,客户端在应用程序的多个子域之间移动,但访问相同的网站。子域区分不同的公司,一个用户与多个公司关联。使用cookie跨这些域跟踪用户的信息。
我的问题是,尽管在两个子域上存在相同的cookie ID,但如果我将信息存储在a.example.com上的cookie中,然后在b.example.com上加载相同的页面和cookie,它不会显示这些信息。根据之前对SO的回答,我将cookie设置为对.example.com上的/有效。
我为我的应用程序打开了实例伸缩,并认为这可能会导致问题,我关闭了它。但是即使有一个实例,Azure仍然有两个不同的主机为不同的域服务请求-尽管相同的实例,因此应该是相同的共享会话。
我使用下面的代码作为我在两个子域上访问的页面,这些子域应该打印出我用cookie访问过的所有子域,但它只显示当前域。
$currentCookieParams = session_get_cookie_params();
$serverParts = explode('.', $_SERVER['HTTP_HOST']);
$serverPartsCount = sizeof($serverParts);
session_set_cookie_params(
time() + 315360000, //(10 * 365 * 24 * 60 * 60),
'/',
'.'.$serverParts[$serverPartsCount-2].'.'.$serverParts[$serverPartsCount-1], //equates to '.mydomain.com'
TRUE,
$currentCookieParams["httponly"]
);
session_start();
//use an array to track every subdomain we visit with this cookie
if (!isset($_SESSION['visitedDomains']))
{
$_SESSION['visitedDomains'] = [];
}
$_SESSION['visitedDomains'][$_SERVER['HTTP_HOST']] = 'visited';
//show the cookie id
var_dump(session_id());
echo '<br><br>';
//the hostname of the machine serving this request
var_dump(gethostname());
echo '<br><br>';
//what should be a list of all domains visited using this cookie
var_dump($_SESSION['visitedDomains']);
echo '<br><br>';
//cookie params
var_dump(session_get_cookie_params());发布于 2017-11-13 17:07:40
您需要将session.cookie_secure设置为FLASE。如下所示:
session_set_cookie_params(
time() + 315360000, //(10 * 365 * 24 * 60 * 60),
'/',
'.'.$serverParts[$serverPartsCount-2].'.'.$serverParts[$serverPartsCount-1], //equates to '.mydomain.com'
FLASE,
$currentCookieParams["httponly"]
);它对我来说很好:

我使用的代码如下:
<?php
$currentCookieParams = session_get_cookie_params();
$serverParts = explode('.', $_SERVER['HTTP_HOST']);
$serverPartsCount = sizeof($serverParts);
echo '.'.$serverParts[$serverPartsCount-2].'.'.$serverParts[$serverPartsCount-1];
echo '<br><br>';
session_set_cookie_params(
time() + 315360000, //(10 * 365 * 24 * 60 * 60),
'/',
'.'.$serverParts[$serverPartsCount-2].'.'.$serverParts[$serverPartsCount-1], //equates to '.mydomain.com'
FALSE,
$currentCookieParams["httponly"]
);
session_start();
//use an array to track every subdomain we visit with this cookie
if (!isset($_SESSION['visitedDomains']))
{
$_SESSION['visitedDomains'] = [];
}
$_SESSION['visitedDomains'][$_SERVER['HTTP_HOST']] = 'visited';
//show the cookie id
var_dump(session_id());
echo '<br><br>';
//the hostname of the machine serving this request
var_dump(gethostname());
echo '<br><br>';
//what should be a list of all domains visited using this cookie
var_dump($_SESSION['visitedDomains']);
echo '<br><br>';
//cookie params
var_dump(session_get_cookie_params());https://stackoverflow.com/questions/47245846
复制相似问题