简体   繁体   中英

Including a PHP file of another server

I am trying to communicate between two servers through PHP. Lets say, there is one PHP file "a.php" on my localhost and another PHP file "b.php" on a remote server. I want to include b.php in a.php. I am trying to do this through include method by giving a full path of remore server " http://ip/b.php " but nothing happens. Actually I want to run a part of script from a.php file then I want to communicate with b.php file and then return back to a.php file. Please guide me how to do this. I know there are similar questions asked and I have tried to resolve this issue using those techniques but in vain.

Thank you

Nope, this setting is disabled/not allowed by default in most web servers (php.ini) so you can not use the include to include the files from a remote addresss for security reasons.

If you still want to allow inclusion of remote files, the directive allow_url_include must be set to On in php.ini

But again it is a bad practice, in a security-oriented point of view ; and, so, it is generally disabled (I've never seen it enabled, actually)

If you want to read the contents of a remote file though, you can use the file_get_contents function instead BUT this will be returned as pure HTML markup code, there won't be any server-side code.

including php file from another server with php

Another Solution is

  1. Save your file as a text file removing the from it.
  2. Access the file using file_get_contents

Example

http://ip/b.php - save this file as b.txt

<?php 
    $filedata = file_get_contents('http://ip/b.txt');
    eval ("?>$filedata");
?>

As much as it is unsafe and bad practice, you can always turn off php for particular directory, using .htaccess (php_flag engine off).

Then your files will be served directly as a text files to whoever know url. That way you can include them via allow_url_include or as Mad Angle said get_file_contets.

But either way - its bad idea. So you can try it for science :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM