Forum Moderators: coopster
include("http://www.domain.com/script.php");
echo $variable; // $variable still empty?
The $variable is created inside script.php but is empty when I echo it from the calling script.
Is it the way I'm including the script via http? I have to do this since it's on another server.
In other words, just because it's a slow server, for example, PHP can't start processing until it has the whole script, especially not since PHP4. So you should either get some error if the include fails or the include should work, at which point it is effectively the same as if it were all one big file.
So, do you know that the file is actually getting included?
Also, do you have error_reporting set to E_ALL?
I ended up find a workaround for this, displaying the variable at a different point in the output than I would have preferred but at least it works.
Thanks everyone for your help. If I get some time I'll play around with it a bit more. Any fixes I'll post back here.
PHP documentation on include [us4.php.net]
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Appendix L for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
Emphasis mine.
So, if possible, you should include the file via a regular file path. Otherwise, if you *really need* something set in script.php in another file, I'd change script.php to output a simple xml packet, something like the following.
<packet>
<results>[HTML results here]</results>
<variable>[value you want here]</variable>
</packet> Then, parse the packet and print out the results, and populate a variable with <variable>. Kind of hacky, but that's the only way I see to do what you want.