Laravel 会话通过 Ajax 调用自动延长?
Laravel session automatically extended by an Ajax call?
我有一个小的 JS 脚本 运行 在一个区间内:
<script>
function checkAuthState() {
$.get('/session/check', function(response) {
console.log(response);
});
}
setInterval(checkAuthState, 10000);
</script>
控制器动作目前是一个虚拟动作:
<?php
public function checkAuthState() {
return response()->json(array('success' => true));
}
我注意到,每次调用 Ajax 时 laravel_session
cookie 过期日期 都会自动延长 - 10 秒(与 JS 间隔匹配)。
为什么会这样?
我需要为会话过期的情况添加一个处理程序,但显然每 10 秒访问一次服务器会阻止(?)它。我很困惑。
我的 session.php 配置中的重要设置:
'lifetime' => 1, /* 1 min, for testing purposes */
'expire_on_close' => false,
当访问者看起来已经离开站点并离开时,会话过期。假定为 "A certain amount of time after they last made a request to the site".
Ajax 请求是一个请求(就服务器而言与其他任何请求一样),因此 cookie 会像其他任何请求一样被扩展。
这就是会话的工作原理。如果要存储一些应该过期的数据,请改用 cache。存储数据一分钟:
cache(['key' => 'value'], 1);
然后使用它:
public function checkAuthState()
{
cache('key');
return response()->json(array('success' => true));
}
更新
如果你想知道会话是否过期,只需存储一些值:
session('key', 'value')
并检查它:
if (session('key'))
我有一个小的 JS 脚本 运行 在一个区间内:
<script>
function checkAuthState() {
$.get('/session/check', function(response) {
console.log(response);
});
}
setInterval(checkAuthState, 10000);
</script>
控制器动作目前是一个虚拟动作:
<?php
public function checkAuthState() {
return response()->json(array('success' => true));
}
我注意到,每次调用 Ajax 时 laravel_session
cookie 过期日期 都会自动延长 - 10 秒(与 JS 间隔匹配)。
为什么会这样?
我需要为会话过期的情况添加一个处理程序,但显然每 10 秒访问一次服务器会阻止(?)它。我很困惑。
我的 session.php 配置中的重要设置:
'lifetime' => 1, /* 1 min, for testing purposes */
'expire_on_close' => false,
当访问者看起来已经离开站点并离开时,会话过期。假定为 "A certain amount of time after they last made a request to the site".
Ajax 请求是一个请求(就服务器而言与其他任何请求一样),因此 cookie 会像其他任何请求一样被扩展。
这就是会话的工作原理。如果要存储一些应该过期的数据,请改用 cache。存储数据一分钟:
cache(['key' => 'value'], 1);
然后使用它:
public function checkAuthState()
{
cache('key');
return response()->json(array('success' => true));
}
更新
如果你想知道会话是否过期,只需存储一些值:
session('key', 'value')
并检查它:
if (session('key'))