views:

135

answers:

3

Hello, I'm using this function to determine whether my application should be online or offline:

function online() {
   if ($online == "0") {
     if($_SESSION['exp_user']['userlevel'] != "1") {
          include("error/offline.php");
          exit();
                                                   } 
                        }
                   }

However, with the data value set to 0 in the database, and $online does = '0', why is error/offline.php not included for those whoose user level is not 1?

Thanks :)

+7  A: 

What is $online, a global variable? If so you have to do global $online to access it inside a function. Right now $online is a default null value, which is not equal to string "0".

chaos
$online is a global variable. How do I make it global to access it in a function?
Shamil
You write global $online in your function, before using $online. That makes $online as it exists in your function refer to the same thing as $online globally.
chaos
Thanks. I have learned something new today!
Shamil
+1  A: 

"Chaos" is right about the global variables. But if you're not sure, one way to debug something like this is to add "echo" or "die" statements in various places, to see what's happening in the code. Put one inside the first "if" statement to see if it gets that far, then one in the second "if" statement. Echo the values of the variables you're testing, so you can tell why the conditions aren't working.

JW
Thanks, I usually debug by adding echos :P
Shamil
+1  A: 

To JW's point for debugging. Instead of littering your code with echos though just make a quick class such as Logger or Debug that you can call to log messages as echos. Or better yet use an exisitng tool such as http://www.indelible.org/php/Log/guide.html. This will let you debug in [FirePHP in Firefox][2] and never have to clean up echo statements again. Or just use Firebug directly if you only plan on using it for debug in browser iteration testing.

You can clean them all up later or use it as a code logger which should be in most larger applications for error logging and reporting metrics.

Ryan Christensen