简体   繁体   English

包括另一个服务器的PHP文件

[英]Including a PHP file of another server

I am trying to communicate between two servers through PHP. 我正在尝试通过PHP在两台服务器之间进行通信。 Lets say, there is one PHP file "a.php" on my localhost and another PHP file "b.php" on a remote server. 可以说,我的本地主机上有一个PHP文件“ a.php”,而在远程服务器上有另一个PHP文件“ b.php”。 I want to include b.php in a.php. 我想在a.php中包含b.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. 我正在尝试通过包含方法来提供remore服务器“ http://ip/b.php ”的完整路径,但是没有任何反应。 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. 实际上,我想从a.php文件运行脚本的一部分,然后与b.php文件通信,然后返回到a.php文件。 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. 不,此设置在大多数Web服务器(php.ini)中默认为禁用/不允许,因此出于安全原因,您不能使用include包含来自远程地址的文件。

If you still want to allow inclusion of remote files, the directive allow_url_include must be set to On in php.ini 如果仍然要允许包含远程文件,则必须在php.ini中将指令allow_url_include设置为On。

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. 但是,如果您想读取远程文件的内容,则可以使用file_get_contents函数来代替,但是它将以纯HTML标记代码的形式返回,不会有任何服务器端代码。

including php file from another server with php 包括来自另一个服务器的php文件和php

Another Solution is 另一个解决方案是

  1. Save your file as a text file removing the from it. 将文件另存为文本文件,然后从中删除。
  2. Access the file using file_get_contents 使用file_get_contents访问文件

Example

http://ip/b.php - save this file as b.txt http://ip/b.php-将此文件另存为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). 尽管这是不安全和不道德的做法,但您始终可以使用.htaccess(关闭php_flag引擎)关闭特定目录的php。

Then your files will be served directly as a text files to whoever know url. 然后,您的文件将直接作为文本文件提供给任何知道url的人。 That way you can include them via allow_url_include or as Mad Angle said get_file_contets. 这样,您可以通过allow_url_include或如Mad Angle所说的get_file_contets包括它们。

But either way - its bad idea. 但是无论哪种方式-这都是一个坏主意。 So you can try it for science :) 所以你可以尝试科学:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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