views:

512

answers:

4

This should work, so I'm really perplexed about why it's not working.

I'm checking to see if a user is logged in using a $session class method at the top of each admin page. However, I want to dynamically adjust the header file depending on whether a user is logged in, and on what role level that user has.

So I check $session->is_logged_in() and then I went ahead and defined a variable called $logged = true; to use later. I then use a $user->find_by_id($session->id) method to create a new User object and store $user->role_level in a new var called $role_level for easy use.

This is all happening near the top of the page. Further down the page past form processing, etc., is the include("../_layouts/header.php") command. Then, in the header.php file, I use little checks like if(!$logged) { ... } else { ... }. However...

I am getting the following errors:

Notice: Undefined variable: logged in /home/hips/html/_layouts/header.php on line 119
Notice: Undefined variable: logged in /home/hips/html/_layouts/header.php on line 131
Notice: Undefined variable: logged in /home/hips/html/_layouts/header.php on line 138

How can this be? I'm defining the vars in the file before I include header.php! Shouldn't that work?

FYI, everything was working fine until I tried to use $logged in the header.php file.

+2  A: 

Variables do propagate to the included files, so it must be, that the variable is NOT set when you call the include: try checking if so, then figure out why is the variable not set at that point.

For example, if you defined $logged inside the "if" block or inside a function, then it won't propagate outside of it: you must define it in the outermost scope (at the same level at which you call the include statement). And you must define it for the case that the user is not logged in, not only for the case when the user is logged in. If the variable is not initialized to false, the check if(!$logged) will issue warning. Say $logged = false; at the beginning of your work.

Palantir
Thanks to you both. The $logged variable is set at the top of the file as $logged = false; -- it's very confusing.
Jason Rhodes
It must have been some kind of glitch because it's working now. Thanks everyone.
Jason Rhodes
+1  A: 

Hi,

Try something very simple, like

// file1.php
$var = "foobar";

// file2.php
include("file1.php"); // or require("file1.php");
die($var);

Does it work? Maybe it's a problem outside your code.

Gastón
+1  A: 

Just an opinion... instead of including code to execute in the global space and depending on another global variable being defined, include your file wherever and have the code inside header.php be organized into appropriate functions. Then where you want the output from header.php, call the appropriate function with the $logged as an argument. This will help make your code more cohesive and easier to test (as in unit tests).

grantwparks
I'm not sure I follow but I'm interested in what you're saying. I'm storing user data in a Session so I just need to be able to alter the Header HTML based on the user's role/permissions. When header.php seemed unable to call the $session variable I tried declaring $logged. It looks like there was a glitch that prevented me from doing either, so I can now probably go back to just calling $session-> etc. What were you suggesting?
Jason Rhodes
It's not related specifically to any problem you might have been having, but is more related to the common practice of having a file with global code in it (meaning the code isn't inside any function) and including that file where you want its output to appear in the page. What I'm suggesting instead, is that you wrap that code in a function. Instead of the code in that file using variables that are expected to be defined in the containing page, you rewrite it slightly so the code is using the function's arguments. Then you can include that file at the top of page (or anywhere) and call it.
grantwparks
...so you might have `function getPageHeader($bIsLoggedIn) { do stuff based on $bIsLoggedIn and return the HTML as a string; }`. Where you want the output to appear you can `echo getPageHeader($logged);`. This will decouple the code from global variables, etc, and make it reusable and unit testable. It's just safer and less likely to be broken.
grantwparks
A: 

Instead of if (!logged), try if (empty($logged)). That won't generate a notice in cases where the variable hasn't been set.

sherlock42