Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php include + variable

Status
Not open for further replies.

Geronantimo

Technical User
Apr 3, 2006
79
SE
Hi,

I'm sure this is relatively simple, but my search on the forum for the terms "include" and "variable" are probably too vague for me to find the answer.

I have a page with the following include:
Code:
<?php include("upcoming.php"); ?>
This works fine.

I would like to add a variable to the end of the "include" like this:
Code:
<?php include("upcoming.php?user=jon"); ?>
but this does not work. :-(

Going the actual page works fine.

I presume there is an error in the way I am writing the include.

Would anybody be kind enough to point it out to me?
 
That's not quite how include() works. When you call "include('somefile.php')", it will operate on the local filesystem, opening the file and executing the PHP code in the current scope. If you have allow_url_fopen enabled, it is also possible to pass a URL to include(). However, in that case, it will just return the HTML generated by the script and not give you access to the PHP code in it.

So the question is: what are you trying to do? If you have a variable in the script named $user, then you can just set it before the include call:
Code:
$user = 'jon';
include('upcoming.php');
If you just want to get the output of the URL, you'll need to pass an actual URL like this:
Code:
include('[URL unfurl="true"]http://yourhost.com/upcoming.php?user=jon');[/URL]
Of course, in the latter case, your server has to be configured with the URL fopen wrappers.
 
Why don't you set the variable in the included file it self.
 
not derogating from either of the above posts, this code

Code:
<?php include("upcoming.php?user=jon"); ?>

is not far wrong. to make it behave the way you want you need to specifiy only that the transport portocol is one that will evaluate php. ie http.

so better to do

Code:
<?php include "[URL unfurl="true"]http://www.domain.com/upcoming.php?user=jon";[/URL] ?>

you must have allow_url_fopen turned on in php.ini
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top