I have a page 1.php?id=4 and in the 1.php page I set $id = $_REQUEST['id']; In the included page, 2.php I want to be able to use $id also. I have tried everything from even putting a query string in the included file, and still nothing works.
Well, its kind of tough to tell from your message what exactly you're trying to do. Plus, I'm pretty new around here, just trying to give back to a community that helps me a bunch. Could you tell us if you're getting an error message or what? As far as I know, something like this should work for page 1.php: <? //code for page 1 $id = $_REQUEST['id']; include("2.php"); ?> PHP: If you place the include after you create the variable, it should work. Also, I think this should work to: <? //code for page 1 $id = $_REQUEST['id']; include("2.php?id=$id"); ?> PHP: I'm not completely sure about this, but I think I'm right, maybe someone with a little more experience could chime in...
I get no error, nothin appears because it is just some php code. This is a page that includes 1 of 2 pages, one will show when logged in another will show when logged out... I have tried both of these: $id = blah; include(2.php?id=$id); PHP: id = $blah; include(2.php?id={$id}); PHP: I have just trying including the page, and just going ahead and using $id within 2.php, but still nothing??... fsmedia, that did not work, the files being included are in another directory, while the page calling on the included ones is in the root? if that makes any difference..?
To make sure you're getting the right variable, you may try <?php print_r($_REQUEST['id']); ?> Code (markup):
I am not certain about your case, but I recently solved a similar problem by changing the extension of the included file to .inc. Seems the php file attempts to render itself as html before being included. Whereas the .inc file passes the php code to the first document as is. I am not certain if this was due to an unusual server configuration on my end or if it is default but it worked for me... Please let me know if this helps.
Thanks for your help, I accidently goofed up on some variable name when querying the db and that's why it kept returning blank
In 1.php it should just be: include("2.php"); without "?id=$id". Then in 2.php you can access id the same way as in 1.php: $_GET['id']; or $_REQUEST['id'];.
Just wanted to say that it is not safe to use extensions like .INC etc. unless you have restricted access to them using .HTACCESS or something. Otherwise, anyone can view the source of that include file by just viewing it in a browser. Just my $0.02 Thomas
When using PHP include, you CANNOT pass arguments with ? . This is because the included PHP files are not called through HTTP but the code in them is placed in the place of the include call, in the source code. So it should be possible to access the $id variable directly from within 2.php . Uless 2.php contains only functions. In that case, you will have to declare $id as global in each function <?php // 2.php function foobar1() { global $id; // blah blah... } function foobar2() { global $id; // blih blih... } ?> PHP: Hope that made sense. Thomas
The $id variable should be availible in the included PHP-File as well. Try the following code. $id = $_REQUEST['id']; // check if the variable is really set: echo ("The id: ".$id); include("2.php"); //or: require ("2.php"); HTML: