Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing PHP variables to an included file?

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.

like image 703
rhodesjason Avatar asked Oct 21 '09 14:10

rhodesjason


People also ask

How use variable from include file in PHP?

In case you need to access your variable $name within a function, you need to say "global $name;" at the beginning of that function. You need to repeat this for each function in the same file. In other words: write global $var before you use it in another function.

What does include () do in PHP?

PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is allowed to be placed in a PHP include file?

Including a PHP File into Another PHP File The include() and require() statement allow you to include the code contained in a PHP file within another PHP file. Including a file produces the same result as copying the script from the file specified and pasted into the location where it is called.


2 Answers

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.

like image 168
Palantir Avatar answered Sep 30 '22 15:09

Palantir


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.

like image 34
Gaston Avatar answered Sep 30 '22 14:09

Gaston