Question

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?

Was it helpful?

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;
   }
}

OTHER TIPS

The fact that $site is false, could being caused by problem with the query. Change the mysql code to:

$result = mysql_query("SELECT * FROM problems");
if(!$result) {
    die(mysql_error();
}

$site = mysql_fetch_object($result);

Further you should learn howto enable error messages in PHP. I guess there are bunch of them. They are disabled by default as it could be a security risk in a production system. But when you are developing you MUST enable them. You can enable it in the php.ini of development system:

php.ini:

...
display_errors=1
...
log_errors=1
...
error_log="/path/to/writable/file"
...
error_reporting=E_ALL

After modifying the php.ini don't forget to restart the web server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top