Question

I am losing my varible $user once I call my error function and include the footer file. I use the $user function in my footer to display some information, but when I went to var_dump it, it just says null. Note, I am using a database framework that someone created, so the query might look weird, but it is 100% functional and secured.

This is in my /includes/functions file:

$user_query = $db->query("SELECT * FROM `users` WHERE `id`='$_COOKIE[id]' LIMIT 1");
$user = $db->getArray($user_query);

function error($input){

            echo '<div id="error_box"><h1>Ooops!</h1><br>
             '.$input.'<br /><a href="javascript:history.go(-1)"><b>Back to previous page.</b></a></div>';

   require_once('./layout/footer.php');
   die;

}

and my /layout/footer.php file is:

         if (!empty($user['active'])){

           //info here

         }
         else {

            //info here

            var_dump($user);

         }
Was it helpful?

Solution

$user is out of scope in error(). A quick fix is to global it. So:

function error($input){
   global $user;
   // ...
}

It may be worth reading up on variable scope in general.

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