我将Symfony应用程序从Symfony 4.0.7升级到Symfony 4.1,之后AJAX调用正在丢失会话值。
我同时调用了大约6个ajax请求。首先,它们进展良好,但另一些则失去了会话值。它只在迁移到Symfony 4.1之后才发生,并且只发生在AJAX调用中。有什么想法吗?
编辑:只有同时调用ajax时才会发生这种情况。WHen,我在调用ajax之间增加了100毫秒的延迟,然后一切正常工作。
edit2:它发生在4台不同的服务器上。2台开发服务器,1台测试服务器和1台活动服务器。它们都运行在NGINX和php7上。
发布于 2018-09-07 09:29:33
好的,问题在于会话固定策略,它改变了每个请求的会话ID,AJAX请求也有时间更新新的ID。
解决方案非常简单,只需设置session_fixation_strategy: none。
发布于 2018-06-23 10:21:24
可能的原因如下:
允许缓存使用会话的请求:
每当会话在请求期间启动时,Symfony将响应转换为私有的不可缓存的响应,以防止泄漏私有信息。但是,在某些情况下,即使是使用会话的请求也可以缓存。
例如,可以为属于该组的所有用户缓存与某个用户组相关的信息。处理这些高级缓存场景超出了Symfony的范围,但是可以使用FOSHttpCacheBundle解决它们。
为了禁用使使用会话的请求不可缓存的默认Symfony行为,在Symfony 4.1中,我们添加了可以添加到响应中的NO_AUTO_CACHE_CONTROL_HEADER头:
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');不推荐使用请求::getSession()
使用请求::getSession(),当不存在会话时,Symfony 4.1中不再推荐它,它将在Symfony 5.0中抛出一个异常。解决方案是始终首先检查会话是否存在请求::hasSession()方法:
if ($request->hasSession() && ($session = $request->getSession())) {
$session->set('some_key', 'some_value');
}更多关于参考文献:这里。
发布于 2018-06-27 04:21:26
你查过XMLHttpRequest.withCredentials了吗
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
那么响应头呢?
这些请求的会话id是否相等?
http://php.net/manual/en/function.session-id.php
也许您需要在ajax请求之前设置会话cookie。
这里有一个例子:
ajax.php
<?php
function getSessionIdentifier()
{
if (!session_id()) {
session_start();
}
if (!isset($_SESSION['identifier'])) {
$_SESSION['identifier'] = bin2hex(random_bytes(5));
}
return $_SESSION['identifier'];
}
echo json_encode([
'identifier' => getSessionIdentifier()
]);start-without-session.php
<!DOCTYPE html>
<html>
<head><title>start without session</title></head>
<body>
<script>
let started;
const cookie = () => '[' + document.cookie + ']';
const track = () => {
started = started || (new Date()).getTime();
return ((new Date()).getTime() - started) + 'ms';
};
const send = (index) => {
const req = new XMLHttpRequest();
req.open('GET', '/ajax.php');
console.log(track(), 'send', index, cookie());
req.addEventListener("load", () => {
console.log(track(), 'receive', index, cookie(), req.responseText);
});
req.send();
}
document.cookie = "PHPSESSID=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
console.log(track(), 'begin', cookie());
const len1 = 5;
const len2 = 10;
Array.from({length: len1}).forEach((v, i) => send(i));
console.log(track(), 'delay');
Array.from({length: len2}).forEach((v, j) => window.setTimeout(() => send(j + len1), j * 10));
</script>
</body></html>

start-with-session.php
<?php
session_start();
$_SESSION['identifier'] = bin2hex(random_bytes(5));
?><!DOCTYPE html>
<html>
<head><title>start with session</title></head>
<body>
<script>
let started;
const cookie = () => '[' + document.cookie + ']';
const track = () => {
started = started || (new Date()).getTime();
return ((new Date()).getTime() - started) + 'ms';
};
const send = (index) => {
const req = new XMLHttpRequest();
req.open('GET', '/ajax.php');
console.log(track(), 'send', index, cookie());
req.addEventListener("load", () => {
console.log(track(), 'receive', index, cookie(), req.responseText);
});
req.send();
}
//
// document.cookie = "PHPSESSID=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
//
console.log(track(), 'begin', cookie());
const len1 = 5;
const len2 = 10;
Array.from({length: len1}).forEach((v, i) => send(i));
console.log(track(), 'delay');
Array.from({length: len2}).forEach((v, j) => window.setTimeout(() => send(j + len1), j * 10));
</script>
</body></html>

我不能百分之百确定这是同样的情况。需要更多信息。
https://stackoverflow.com/questions/50925212
复制相似问题