gpt4 book ai didi

javascript - 如果在后台使用 jquery 检查 session 失败,如何重定向 index.php 页面?

转载 作者:搜寻专家 更新时间:2023-10-31 20:34:57 24 4
gpt4 key购买 nike

我有一个需要 session 的 index.php。我还有 sessioncheckpage.php

看起来像下面的代码。它运行良好。

$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>

以上是 index.php 中包含的 jQuery。也可以将 index.php 作为 sessioncheckpage.php 重新加载,但是有视频内容,因此视频在页面刷新( session 检查)后不应停止....

最佳答案

可以查看状态码

PHP 代码

<?PHP 
if(notLoggedIN()){
header("HTTP/1.1 401 Unauthorized");
exit;
}
?>

jQuery

$.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");
}
}
});

关于javascript - 如果在后台使用 jquery 检查 session 失败,如何重定向 index.php 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com