Forum Moderators: coopster

Message Too Old, No Replies

Retrieving variables from included file

         

JohnKelly

5:01 pm on May 11, 2005 (gmt 0)

10+ Year Member



I'm trying to use a variable created in a file I'm including from within a PHP script:

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.

kazecoder

5:04 pm on May 11, 2005 (gmt 0)

10+ Year Member



I don't believe so. Is the $variable unique to a function in the script you are including?

JohnKelly

5:21 pm on May 11, 2005 (gmt 0)

10+ Year Member



the $variable is unique in the script being called, so it's not being emptied out elsewhere in the script.

jatar_k

5:38 pm on May 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is it defined in a function so it could be a scope problem?

JohnKelly

1:29 am on May 12, 2005 (gmt 0)

10+ Year Member



I don't believe it is a scope problem, I've tested it and the variable is recognized within the script being called, but not in the calling script. I've checked other variables as well and they are also not passed over to the calling script.

ergophobe

3:28 pm on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think it has to be one of two things
- a scope problem
- failure to actually include the file.

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?

JohnKelly

2:15 pm on May 13, 2005 (gmt 0)

10+ Year Member



The file is definately being included, since it spits out HTML code that I can see.

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.

gliff

5:12 pm on May 13, 2005 (gmt 0)

10+ Year Member



I may be wrong, but I don't belive it's possible to do what you want when you're including a remote file with "URL fopen wrappers".

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.

JohnKelly

8:54 pm on May 13, 2005 (gmt 0)

10+ Year Member



Good idea gliff, I hadn't thought of that. Thanks!

ergophobe

8:00 pm on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks gliff. That explains it. So I have to retract... I think it has to be one of three things.