简体   繁体   中英

Reload/redirect page from included php file

My application has the following structure:

main.php it includes the config.php that has the credentials to the database.

page1.php wich also includes the config.php and calls get_page1_content.php through AJAX.

get_page1_content.php wich also includes the config.php and is called through ajax every time page1.php needs.

My url is allways www.mysite.com/main.php and I load all the pagesX.php files through JavaScript.

example of page1.php, get_page1_content.php and main.php first lines:

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

I want to check if the php session is UP and if it's not I want to refresh the page. Problem is that all my files include the config.php

for now in config.php I have the following __construct()function:

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

But this doesn't seem to work. I can see i get the tag to refresh in response to get_page1_content.php but the browser doesn't refresh.

Is my architecture wrong? How can I fix it?

Thank you in advance.

You can refresh the page with this :

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

This will refresh the page every 0 seconds , So , if you if condition is true , then the page refreshes

You can check the session in your get_page1_content.php and return an error message if the session is expired. In your AJAX script, check for an error and use document.location.reload(true) if necessary. See https://developer.mozilla.org/en-US/docs/Web/API/Location.reload for more information.

Since I had a lot of ajax going on, I've added this function that is called every time ther is an AJAX error.

// 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();
    }
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM