Returning a Variable From A Remote Script--Help!

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
noorodeen
Forum Newbie
Posts: 4
Joined: Sun Jan 07, 2007 2:57 pm

Returning a Variable From A Remote Script--Help!

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
Last edited by Kieran Huggins on Sun Jan 07, 2007 3:59 pm, edited 1 time in total.
noorodeen
Forum Newbie
Posts: 4
Joined: Sun Jan 07, 2007 2:57 pm

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

sorry - had a typo in my previous post (now fixed):

Do something like this:

Code: Select all

echo '$variable = "'.$value.'";';
noorodeen
Forum Newbie
Posts: 4
Joined: Sun Jan 07, 2007 2:57 pm

Post by noorodeen »

Hey, you're awesome. It worked. I've been trying different things all day.....

Thanks a lot.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Hyper
Forum Newbie
Posts: 2
Joined: Mon Jan 15, 2007 12:10 pm

Post 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);
Post Reply