从包含的php文件重新加载/重定向页面


Reload/redirect page from included php file

我的应用程序具有以下结构:

main.php它包括具有数据库凭据的config.php。

page1.phpwich还包括config.php,并通过AJAX调用get_page1_content.php。

get_page1_content.phpwich还包括config.php,每次page1.php需要时都会通过ajax调用它。

我的网址是www.mysite.com/main.php,我通过JavaScript加载所有pagesX.php文件。

page1.php、get_page1_content.php和main.php第一行的示例:

include("config.php");
$conf=new config();

我想检查php会话是否为UP,如果不是,我想刷新页面。问题是我的所有文件都包含config.php

目前在config.php中,我有以下__construct()函数:

    if (isset($_SESSION['@ADMIN'])){
        $result=true;
    }
    else {
        echo "<meta http-equiv='"refresh'" content='"0'">";
    }

但这似乎不起作用。我可以看到,我让标签刷新以响应get_page1_content.php,但浏览器不刷新。

我的架构有错吗?我该怎么修?

提前谢谢。

您可以使用以下内容刷新页面:

   $page = $_SERVER['PHP_SELF'];
   $sec = "0";
   header("Refresh: $sec; url=$page");

这将每0秒刷新一次页面,因此,如果如果条件为true,则页面将刷新

您可以在get_page1_content.php中检查会话,如果会话已过期,则返回错误消息。在AJAX脚本中,检查是否有错误,并在必要时使用document.location.reload(true)。看见https://developer.mozilla.org/en-US/docs/Web/API/Location.reload了解更多信息。

由于我有很多ajax,我添加了这个函数,每当出现ajax错误时都会调用它。

// Deal with ajax calls errors        
$( document ).ajaxError(function(e) {
    if ($.cookie('sitecookie')!=null){
        console.log("cookie exists");
        console.log($.cookie('sitecookie'));
        console.log(e);
    } else {
        alert("Your session expired. Please log in again.");
        location.reload();
    }
});