Page 1 of 1

[SOLVED] Include with Variables

Posted: Tue Aug 31, 2004 12:18 pm
by AliasBDI
I have page with this code:

page.php?domain=name

Code: Select all

<?php $domain = $_GET['domain']; $redirect = "?domain=" . $domain; ?>
<?php include '../includes/mod_users.php$redirect'; ?>
The two PHP codes are actually inside of HTML code, that is why they are separated.

So, when I echo the second line, it comes out like this:

Code: Select all

include '../includes/mod_users.php?domain=name'
That is correct. However, when the actual page runs as an include, the variable is not working, it comes out like:

Code: Select all

include '../includes/mod_users.php$redirect'
What am I doing wrong? The purpose is to pass a variable from include to include, because each include needs a URL variable. Any ideas?

Posted: Tue Aug 31, 2004 12:26 pm
by markl999
You don't need to add variables onto the end of the included file name. Included files automatically 'inherit' the variables of the file that included it:
Eg

Code: Select all

$foo = 'hello';
include_once 'somefile.php'; //somefile.php can now access $foo as normal
But your specific problem is that you used single quotes here:
<?php include '../includes/mod_users.php$redirect'; ?>
PHP does not interpolate (evaluate) variables inside single quotes, so you need to use double quotes, though as i said above you don't need to 'pass' $redirect to the included file, it automatically has access to it.

Posted: Tue Aug 31, 2004 1:19 pm
by AliasBDI
Right. It works now. Thanks.