Forum Moderators: coopster

Message Too Old, No Replies

How can I include PHP remotely?

I know this is not impossible, I have seen it done.

         

haryanto

10:52 pm on Aug 8, 2004 (gmt 0)

10+ Year Member



Hi guys.

If I have say a plain text file on a server containing 'phpinfo();'

And I wanted a PHP on another server to read that string and executing it as a PHP. I saw some pages sometime ago that has a working code to implement that.
I was like "Interesting. But I have no need for that....click onnnn" so I surfed away without bookmarking. Now that I need it I couldn't find that page.

Timotheos

3:19 pm on Aug 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The manual has a section called Using Remote Files [php.net]. Depending on your version you can put a URL address in various file functions like include().

haryanto

4:39 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



I think you got it wrong.

I am aware of that include function.
But it only include the generated code as a normal text. Say for a remote phpinfo();
It will retrieve the html generated by the remote server then it get included in my PHP.
I wanted it to read the code 'phpinfo()' froom the remote server, then execute that command on my machine instead.

lorax

5:07 pm on Aug 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think what you're asking for is to have the server - where the target code lives - give you access to the files before they are processed by the webserver and PHP engine which means you'll either need FTP access or access to some form of API which will give you the code. Any calls to the file directly via http will cause the file to be processed and only the results sent to you.

WhosAWhata

5:13 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



i think FTP is the easiest

coopster

6:26 pm on Aug 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Read the plain text file into a variable, then use eval() [php.net] to process the string. So, let's say you have a file named
text.txt
on your example.com web site and it contains this line...
phpinfo();

This should work...
<?php 
$file_get_contents = file_get_contents('http://example.com/text.txt');
print eval($file_get_contents);
?>

haryanto

1:47 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Coopster,
exactly what I was looking for!

I tried it out only to get an error msg:

Warning: file_get_contents(http://www.myserver.com/phpinfo.txt): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/myserv/public_html/generate.php(39) : eval()'d code on line 1

[edited by: haryanto at 1:56 am (utc) on Aug. 10, 2004]

jollymcfats

1:55 am on Aug 10, 2004 (gmt 0)

10+ Year Member



If the code is in a text file, you can just use include():

<?php include('http://example.com/text.txt');?>

No need for the eval.

haryanto

1:58 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Jolly it will not,
check out third post.

This is not a normal include.
This is including a command on remote text file and executing it on my own server. This makes it far more challenging.

jollymcfats

2:33 am on Aug 10, 2004 (gmt 0)

10+ Year Member



You do need to have
allow_url_fopen
enabled in php.ini. It's enabled on my system by default. If include() is borking for you, try turning this on.

haryanto

3:23 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Oh it works guys!
LOL

Thanks all, especially coopster and jolly!
Have a jlly good day!
YOu guys just made my day

But what if the remote PHP contains strings like

<html>
phpinfo();
</html>

I know the '<' and the '>' will crash my eval()
I tried add slashes to the string but they dont work!

$what = file_get_contents('http://www.example.com/test.phps');
$myString = addslashes($what);
print eval($myString);

TheBlueEyz

11:24 am on Aug 10, 2004 (gmt 0)

10+ Year Member



You'll have to strip any non-php code out.

You might look at a pattern matcher for that, unless you always know exactly what tags will be present in the files (then you can just use str_replace)

coopster

9:53 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or strip_tags() [php.net] may work for you.