0

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?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
user1477332
  • 325
  • 2
  • 4
  • 20

2 Answers2

2

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;
   }
}
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • this not working ... i login to phpmyadmin and i set tinyint value 0 ... it appears tinyint(4) i do not know this normal or what and if i set it to 1 .... tinyint(1) ... no maintenance page – user1477332 May 04 '13 at 13:30
  • @user1477332 It indicates the length of that integer val to be accepted – Mr. Alien May 04 '13 at 13:31
  • then i am doing well in phpmyadmin .. but code not working yet – user1477332 May 04 '13 at 13:32
  • @user1477332 only 1 is also valid – Mr. Alien May 04 '13 at 13:37
  • not the same to me ... just 1 ... appear maintenance.php whatever the value in phpmyadmin 0 or 1 – user1477332 May 04 '13 at 13:42
  • @user1477332 PHP is not a strict language, if you use === than it will fail but using == '1' or == 1 is same thing, PHP will smartly convert that string to integer – Mr. Alien May 04 '13 at 13:44
  • 1
    now it's working with me .. thanks for help man (error was from the row) i just made new rows instead of edit the first row ... thanks again man .... but one last question---> What if user JS is off ?? what kind of error will appear to user ?????? – user1477332 May 04 '13 at 19:15
  • @user1477332 If you are using my code only than nothing will happen even if the user turns off JS :) and you welcome – Mr. Alien May 04 '13 at 20:46
0

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.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266