trouble returning variables from include()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

trouble returning variables from include()

Post by bwv2 »

I am remotely including files on a website by calling their absolute URLs. I am creating arrays inside of these included remote files which I need to use on the client-side page. However, thus far I have been unsuccessful in passing the arrays created in the remote files back to the script. Here's an example of what's happening:

Code: Select all

/*---remoteFile.php---*/
$theArray = array();
//here I successfully pull many rows from a mySQL database and assign them to $theArray...I'll spare the code
return $theArray;
//----------------------//
 
/*---clientFile.php---*/
$showIt = include("http://www.mysite.com/includes/remoteFile.php?arguments=$args");
print "Array: ".$show;
//prints: "Array: 1"
//----------------------// 
The number 1 is printed to the screen because it's saying the include() operation was successful. If it had sent back my array it would have printed "Array: Array". I have also tried it with non-array values such as $foo="it works", etc.

Any idea why this might not be working? I have also tried assigning the text of remoteFile.php to a variable using:

Code: Select all

$showIt = file_get_contents("http://www.mysite.com/includes/remoteFile.php?arguments=$args");
echo $showIt; 
This does work to create tables and whatever else I put in the file, but I can't use the variables created within it on the client side. Any clues?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: trouble returning variables from include()

Post by Christopher »

You are setting the var $showIt, but displaying $show.

Code: Select all

$showIt = include("http://www.mysite.com/includes/remoteFile.php?arguments=$args");
print "Array: ".$show;
(#10850)
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Re: trouble returning variables from include()

Post by bwv2 »

That was a typo in my forum post. The printed variable is the same as the set variable. Sorry.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: trouble returning variables from include()

Post by Christopher »

Just noticed that you are trying to include a remote file. I am assuming that the file is actually on a different server. Does you configuration allow you to do that?
(#10850)
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Re: trouble returning variables from include()

Post by bwv2 »

Yes. I am able to include remote files, and I can pass them variables using GET in the URL string. I have confirmed that the inclusion of remote files does work. The only issue now is using the variables that I set inside of those files. I found through forums that assigning the include() to a variable and returning the desired variable using return() should do the trick (like a javascript function). This is where it all breaks down for me.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: trouble returning variables from include()

Post by Jade »

are you trying to use the variables from those files inside of functions in the file they've being included in? If so you need to make them global.
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Re: trouble returning variables from include()

Post by bwv2 »

The variables are not created in functions. However, I wonder if making them global would make them accessible to the outside? Perhaps not since they're being made on a different server.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: trouble returning variables from include()

Post by Jade »

You could always try declaring them global in the file you're including them in instead of the file you created them in. I'm not sure if that would work but it can't hurt to try.
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Re: trouble returning variables from include()

Post by bwv2 »

I got something to work. In case anyone in the future has this issue, this works:

First, in remoteFile.php, remove <?php and ?> from beginning and end of the file. Then, call this:

Code: Select all

eval(file_get_contents('http://www.mysite.com/includes/remoteFile.php'));
That grabs the code from the remote file without evaluating it, sticks it into your script, and then evaluates it as though it were written there all along. This way you can use preexisting client-side variables, and then use any variables you create within the file afterwards. I guess I just needed to get away from my computer for a while in order to see the answer!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: trouble returning variables from include()

Post by Christopher »

I think you misunderstand what file_get_contents('http://www.mysite.com/includes/remoteFile.php') or include('http://www.mysite.com/includes/remoteFile.php') actually. They don't magically allow you to remotely parse PHP files. The other server is going to parse the PHP and send you the output. So there is not return() statement to execute.

What are you actually trying to do.
(#10850)
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Re: trouble returning variables from include()

Post by bwv2 »

I understand the intent of include() and file_get_contents(), as I use them dozens of times throughout my application. In this particular case I needed to return a variable in addition to the code parsed on the remote server. It is possible to use a return() statement, as described here, from the php manual:
Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.
I was simply having trouble getting this to work in this case. Instead I decided to pull the code from one file to the client computer for parsing rather than parsing on the remote side in order to solve my problem. It worked, so I've moved on. Hopefully this can help someone else if they find themselves stuck and remote parsing is not important.
Post Reply