3

I am finished setting up a maintenance function on my webpage. This is the index.php code

    <?php
        session_start();
        require_once("system/functions.php");
        require_once("system/config.php");
        if($maintenance == 1){
            require_once(header("Location: index.php?page=maintenance"));
            die();
            session_destroy();
        }elseif($maintenance == 0)
        {
            getPage();
        }
    ?>

I have also tried with

    header("Location: index.php?page=maintenance");

Instead of the require once header code above. But if I put

    require_once("frontend/pages/maintenance.php");

It will work. The problem then is that people can type in every page they want in the address bar and this will show up. I need it to use it's own url (Which works with the 2 header codes above, but I get too many redirects error) and no matter what, you will be redirected to this url to see the maintenance screen

The php part of the maintenance.php file:

<?php
if($maintenance == 0){
    header("Location: index.php?page=index");
    die();
}
else{
    header("Location: index.php?page=maintenance");
    die();
}
?>

I can remove the else code part on the maintenance.php file, but then it will always redirect to "websitename"/index.php(Still maintenance screen though, the same problem as mentioned above)

So I need to change my code so when there is maintenance, you will be redirected to index.php?page=maintenance no matter what. Sorry if I missed out on some details, it's late. Feel free to ask me about this, if it is needed :)

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Xsef
  • 53
  • 7

1 Answers1

3

Indeed this looks like you are looping. The following is executed when you are in the index.php script:

require_once(header("Location: index.php?page=maintenance"));

So you actually load the script you are already running, again. And it will again find maintenance==1 and do exactly the same thing again.

You should just redirect once, and then when you see you are already on the page=maintenance URL actually display what you want to display as maintenance message, like this:

session_start();
require_once("system/functions.php");
require_once("system/config.php");
if($maintenance == 1){
    if ($_GET['page']) == 'maintenance') {
        // we have the desired URL in the browser, so now
        // show appropriate maintenance page
        require_once("frontend/pages/maintenance.php");
    } else {
        // destroy session before exiting with die():
        session_destroy();
        header("Location: index.php?page=maintenance");
    }
    die();
}
// no need to test $maintenance is 0 here, the other case already exited
getPage();

Make sure that in frontend/pages/maintenance.php you do not redirect to index.php?page=maintenance or you will still get into loops.

So frontend/pages/maintenance.php should look like this:

// make sure you have not output anything yet with echo/print
// before getting at this point:
if($maintenance == 0){
    header("Location: index.php?page=index");
    die();
}
// "else" is not needed here: the maintenance==0 case already exited

// display the maintenance page here, but don't redirect.
echo "this is the maintenance page";
// ...
trincot
  • 317,000
  • 35
  • 244
  • 286