Page 1 of 1

trouble returning variables from include()

Posted: Wed Feb 27, 2008 10:34 am
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?

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 11:16 am
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;

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 12:33 pm
by bwv2
That was a typo in my forum post. The printed variable is the same as the set variable. Sorry.

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 12:55 pm
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?

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 1:59 pm
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.

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 2:11 pm
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.

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 2:17 pm
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.

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 2:19 pm
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.

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 6:07 pm
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!

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 7:13 pm
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.

Re: trouble returning variables from include()

Posted: Wed Feb 27, 2008 8:45 pm
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.