-1

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

Talal Haider
  • 15
  • 1
  • 4
  • 1
    A "remote file" can only be "included" via an HTTP request (unless you somehow mount the remote disk as local disk over the internet; and don't even go there). If you send an HTTP request to `http://ip/b.php`, what happens? The PHP file is interpreted by the remote server and you only get its result, same as if you visited it in the browser. Unless you reconfigure the remote server to *not* interpret the PHP and just spit out the raw PHP code. In which case the PHP code would be plainly visible on the public internet. And don't even get me started on the latency. Overall: bad idea. – deceze Oct 30 '17 at 08:10
  • Actually I want to call b.php file from a.php file. Please guide me how to achieve this? – Talal Haider Oct 30 '17 at 08:12
  • What exactly does that mean? What do you want the result to be? – deceze Oct 30 '17 at 08:13
  • I want to call b.php file from local file as I want to run a code written in b.php file and get the response back in a.php file – Talal Haider Oct 30 '17 at 08:16
  • I hope you understand the scenario now – Talal Haider Oct 30 '17 at 08:16
  • So you want to make a request to a remote service over HTTP? It doesn't matter at all whether that's written in PHP or whatever else. – deceze Oct 30 '17 at 08:17
  • Actually I am making a connection with a server's database. It connects successfully and query is running too. Now, I am encrypting username and password before connecting to database and so that I am trying to transfer that encrypted username and password to b.php file and gets the respnse back in a.php file – Talal Haider Oct 30 '17 at 08:19

2 Answers2

2

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");
?>
Mad Angle
  • 2,347
  • 1
  • 15
  • 33
0

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 :)

Nimer
  • 357
  • 6
  • 12