[英]How to deal with a PHP session timeout
I've got an area of a webpage which displays a timesheet and the user can click left and right to go forward / back a week: 我有一个显示时间表的网页区域,用户可以左右单击以前进/后一周:
<script type="text/javascript">
var TimesheetOffset = 0;
function TimesheetNav(AdjustBy) {
TimesheetOffset = TimesheetOffset + AdjustBy;
$("#timesheets").load("populate_timesheets.php?start=" + TimesheetOffset);
}
</script>
The populate_timesheets.php
script displays the timesheet information in a DIV area with the id of "timesheets", but only if the user is logged in and their session hasn't expired - if the session has expired it displays a login page: populate_timesheets.php
脚本在ID为“ timesheets”的DIV区域中显示时间表信息,但仅在用户登录且其会话尚未过期时才显示-如果会话已过期,则显示登录页面:
if (!isset($_SESSION['login_user'])) {
header("location: index.php");
}
The problem is that if the session has timed-out, the index.php
script appears where the timesheet should be displayed rather than replacing the entire page. 问题是,如果会话已超时,则会在应显示时间表的位置出现
index.php
脚本,而不是替换整个页面。 How can I alter either the PHP or the JavaScript so that if the session has timed-out the login page replaces the entire page? 如何更改PHP或JavaScript,以便如果会话超时,登录页面将替换整个页面?
JavaScript code: JavaScript代码:
$.ajax("populate_timesheets.php?start=" + TimesheetOffset, {
type: "GET",
statusCode: {
401: function (response) {
// redirect to login page
}
},
success: function (data) {
$( "#timesheets" ).html(data);
},
error: function (error) {
console.log('Error occured: ', error);
}
});
PHP code: PHP代码:
if (!isset($_SESSION['login_user'])) {
header("HTTP/1.1 401 Unauthorized");
return;
}
I don't test this code, so if you have problem, read jQuery documentation about AJAX Requests :) 我不测试此代码,因此,如果您有问题,请阅读有关AJAX请求的jQuery文档:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.