Javascript 如果在后台使用jquery进行会话检查失败,如何重定向index.php页面?
我有一个index.php,它需要会话。我还有sessioncheckpage.php 看起来像下面的代码。它工作得很好Javascript 如果在后台使用jquery进行会话检查失败,如何重定向index.php页面?,javascript,php,jquery,session,Javascript,Php,Jquery,Session,我有一个index.php,它需要会话。我还有sessioncheckpage.php 看起来像下面的代码。它工作得很好 $login_hash=$_SESSION['hash']; if ($login_hash != $tulos) { session_destroy(); header("Location: login.php"); } 但是如何重新加载index.php呢?我的Javascript代码只是希望在后台刷新sessioncheckpage.php <s
$login_hash=$_SESSION['hash'];
if ($login_hash != $tulos) {
session_destroy();
header("Location: login.php");
}
但是如何重新加载index.php呢?我的Javascript代码只是希望在后台刷新sessioncheckpage.php
<script>
var interval = 0;
function start(){
setTimeout( function(){
ajax_function();
interval = 10;
setInterval( function(){
ajax_function();
}, interval * 1000);
}, interval * 1000);
}
function ajax_function(){
$.ajax({
url: "sessionchecker.php",
context: document.body,
success: function(result){
$('.time').html(result);
}
});
}
$( window ).load(function(){
var time = new Date();
interval = 10 - time.getSeconds();
if(interval==10)
interval = 0;
start();
});
</script>
var区间=0;
函数start(){
setTimeout(函数(){
ajax_函数();
间隔=10;
setInterval(函数(){
ajax_函数();
},间隔*1000);
},间隔*1000);
}
函数ajax_function(){
$.ajax({
url:“sessionchecker.php”,
上下文:document.body,
成功:功能(结果){
$('.time').html(结果);
}
});
}
$(窗口)。加载(函数(){
变量时间=新日期();
interval=10-time.getSeconds();
如果(间隔==10)
间隔=0;
start();
});
上面是包含在index.php中的jQuery。也可以将index.php作为sessioncheckpage.php重新加载,但存在视频内容,因此视频在页面刷新后不应停止(会话检查)…如果sessionchecker脚本是RESTful(即实现HTTP错误代码),则添加到
$。ajax
调用错误函数。否则,在success函数中添加一个子句来调用window.location.reload()
您可以检查状态代码 PHP代码
<?PHP
if(notLoggedIN()){
header("HTTP/1.1 401 Unauthorized");
exit;
}
?>
<?PHP
if(notLoggedIN()){
header("HTTP/1.1 401 Unauthorized");
exit;
}
?>
$.ajax({
url: "sessionchecker.php",
context: document.body,
success: function(result){
$('.time').html(result);
}
error: function(jqXHR, status, code){
if (jqXHR.status === 401) {
window.location.replace("/login.php");
}
}
});