Page 1 of 1

Returning a Variable From A Remote Script--Help!

Posted: Sun Jan 07, 2007 3:11 pm
by noorodeen
Hello All,

I've searched quite a bit to try to find answer to this but to no avail. I am hoping you can help me out here.

I am including a remote script in my code and I want the remote script to return a value. How can I do that?

Here's the snippet of code:

Code: Select all

$var = include 'http://www.example.com/file.php?var1=$var1&var2=$var2';
Now, I have tried to get global variables in file.php, I tried using return $var....

Any ideas on how I can this going?

Posted: Sun Jan 07, 2007 3:51 pm
by Kieran Huggins
When you include the page as a URL, it will be processed by php and apache and you will "include" the resulting output. So unless the script outputs something like:

Code: Select all

$variable="value";
you won't get the result you're looking for.

Does that make sense?

Posted: Sun Jan 07, 2007 3:58 pm
by noorodeen
ok. So in other words, if I want to return a value, a number and assign it somewhere in the calling script, how can I do that?

I think I understand what you are saying. You're saying I need to do teh following in my remote .php file

Code: Select all

echo '<?php $var = '.$var."?>";
that's what you are saying right?

Thanks

Posted: Sun Jan 07, 2007 4:01 pm
by Kieran Huggins
sorry - had a typo in my previous post (now fixed):

Do something like this:

Code: Select all

echo '$variable = "'.$value.'";';

Posted: Sun Jan 07, 2007 4:02 pm
by noorodeen
Hey, you're awesome. It worked. I've been trying different things all day.....

Thanks a lot.

Posted: Sun Jan 07, 2007 5:10 pm
by feyd
Take care when requesting a file for inclusion from a remote location. It is often a security hole waiting to be exploited. I would suggest using file_get_contents() and assigning the variable yourself.

Posted: Mon Jan 15, 2007 1:11 pm
by Hyper
Kieran's way results in that line being echoed, not parsed in any way. I don't know why so I used some other way.

remote script which is exporting some useful (not in this case :lol:) data:

Code: Select all

$fruits = array( 'apple', 'banana', );
var_export($fruits); // prints parsable contents of the variable
above $fruits elements can now be recreated (redefined) in other script in following way:

Code: Select all

$imported = file_get_contents("url_here"); // get contents of remote file
eval('$fruits = '.$imported.';'); // $fruits will hold the imported data
print_r($fruits);