Search code examples
javascriptphpmaintenance-mode

PHP Javascript maintenance page


I used this in my index.php

<?
    include('config.php');
    if($site->maintenance > 0){
        echo "<script>document.location.href='maintenance'</script>";
        exit;
    }
?>

and in my config.php after checked database connection

$site = mysql_fetch_object(mysql_query("SELECT * FROM problems"));

I made a table in my database and called it problems but even when I put the value 0, it transfers me to maintenance. When I checked the variable $site by var_dump($site) it outputs:

bool(false)

and if I checked it like this: var_dump($site->maintenance) it outputs:

NULL

What do I have to do to manage the maintenance from database and when I want my site to work I change value?


Solution

  • Why you are using JS for this? What if user JS is off? I would use PHP instead

    Create a table for maintenance with a column say maintenance_status, now this will hold a boolean value, 0 1... 0 => off, 1 => on, will keep only a single record which will be created once, and later will update it always...

    So now later you create this function

    function check_maintenance($connection) { /* Call this function on every page, 
                                              pass your database connection var 
                                              as a function parameter */
       $query = mysqli_fetch_array(mysqli_query($connection, "SELECT * FROM tbl_maintenance LIMIT 1")); 
       /* Limit 1 is optional if you are using only 1 row as I told you, 
          if you are keeping records of the previous maintenance, probably 
          you've to sort desc and use limit 1 */
    
       if($query['tbl_maintenance'] == '1') { //Check boolean value, if it's on than redirect
          header('Location: maintenance.php'); //Redirect the user to maintenance page
          exit;
       }
    }