remote include() question

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
Black Majic
Forum Newbie
Posts: 6
Joined: Sat Apr 12, 2003 11:12 pm
Location: Etobicoke, ON, CA
Contact:

remote include() question

Post by Black Majic »

Is there any way I can have a file hosted remotely which executes a query on the filesystem of the remote server, then return a variable to the calling script?
somthing like...

Code: Select all

include("http://11.22.33.44/remotefile.php") // reads file, returns $fileContents
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

nope, u have to open the file, checkout http://uk2.php.net/manual/en/function.fopen.php :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Uhm...
http://se.php.net/manual/en/function.include.php wrote: If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using an URL (via HTTP or other supported wrapper - see Appendix I 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 an 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.
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

why don't u use the file() function?
i think is $array = file('http://www.thesite.com/page.html');
and then you use the explode function to extract the lines...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

edit: oops, I'm way too slow ;-)

include() returns what the interpreted code of the included file returns.

Code: Select all

<?php // whatSoEver.php
$dirlisting = array();
$dh = opendir('.');
while($de = readdir($dh))
	$dirlisting[] = $de;
	
return $dirlisting;
?>

Code: Select all

<?php
$dirlisting = include('./whatSoEver.php');
print_r($dirlisting);
?>
this will print the array returned by whatSoEver.php

But if you querry a document on another server (via url_wrappers) the script will already be parsed there. Your script does not get any php code it can parse. But you can print something that the local script can parse as php-code

Code: Select all

<?php // whatSoEver.php
$dirlisting = array();
$dh = opendir('.');
while($de = readdir($dh))
	$dirlisting[] = base64_encode($de);
	
echo '<'."?php return '" .serialize($dirlisting) ."'; ?".'>';
?>

Code: Select all

<?php
$returnValue = include('http://your.serv.er/whatSoEver.php');
$dirlisting = unserialize($returnValue);
$dirlisting = array_map('base64_decode', $dirlisting);
print_r($dirlisting);
?>
Black Majic
Forum Newbie
Posts: 6
Joined: Sat Apr 12, 2003 11:12 pm
Location: Etobicoke, ON, CA
Contact:

Post by Black Majic »

the remote script is written to take some constants, and check them against constants on the remote side

then it *should* return a few vars as links to update if the client versions are different from the server versions.
Post Reply