使用 php 包含来自另一台服务器的 php 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2752783/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
including php file from another server with php
提问by SolidSnake
I have two PHP files located on different servers, one at http://www.mysite.com/main.php, the other at http://www.sample.com/includeThis.php.
我在不同的服务器上有两个 PHP 文件,一个在http://www.mysite.com/main.php,另一个在http://www.sample.com/includeThis.php.
I want to include the second file from the first one.
我想包含第一个文件中的第二个文件。
The content of the second file looks like this:
第二个文件的内容如下所示:
<?php
$foo = "this is data from file one";
And the first file:
第一个文件:
<?php
include "http://www.sample.com/includeThis.php";
echo $foo;
Is there any way I can do this?
有什么办法可以做到这一点吗?
回答by Sarfraz
Nope, this setting is disabled/not allowed by default in most web servers (php.ini) so you can not use the includeto include the files from  a remote addresss for security reasons.
不,默认情况下,大多数 Web 服务器 (php.ini) 中禁用/不允许此设置,因此include出于安全原因,您不能使用包含来自远程地址的文件。
If you still want to allow inclusionof remote files, the directive allow_url_includemust be set to Onin php.ini
如果您仍然希望允许包含远程文件,则allow_url_include必须On在 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_contentsfunction instead BUTthis will be returned as pure HTML markup code, there won't be any server-sidecode.
如果你想读,虽然远程文件的内容,你可以使用file_get_contents函数而不是BUT这将返回为纯HTML标记代码,不会有任何服务器端代码。
回答by Pekka
After reading your comments - in which you state that you want to do this as a means of copy protection - my answer is an emphatical, forget it. This is not how copy protection works.
在阅读了您的评论后 - 您声明您希望这样做是为了保护版权 - 我的回答是强调,忘记它。这不是复制保护的工作方式。
The only thing you can do using include()is fetch source code from elsewhere to be interpreted on the local interpreter. This is childishly easy to crack: A malicious customer would to just have to echo()the fetched code.
您唯一可以使用的include()是从别处获取源代码以在本地解释器上进行解释。这很容易破解:恶意客户只需要echo()获取获取的代码即可。
Executing the remote script remotely (on your server) won't help you, because the state of that script (variables, functions...) won't be present in the script you call it from.
远程(在您的服务器上)执行远程脚本对您没有帮助,因为该脚本的状态(变量、函数...)不会出现在您从中调用它的脚本中。
The options you have are:
您拥有的选项是:
Compiling / encoding / obfuscating the script, possibly requiring a specific PHP module to execute it (lots of questions about this on SO)
Creating a real web service (e.g. using SOAP) that runs on your server, and performs the requested operations
编译/编码/混淆脚本,可能需要特定的 PHP 模块来执行它(SO 上有很多关于此的问题)
创建在您的服务器上运行并执行请求的操作的真实 Web 服务(例如使用 SOAP)
For what it's worth, though, I personally do not purchase, nor recommend to clients to purchase, encoded scripts and scripts that need to "phone home" in order to work. I believe in protecting your products through a stringent license agreement (that will scare business customers into buying your product, because the risks of getting caught stealing are too expensive.)
不过,就其价值而言,我个人不购买,也不建议客户购买编码脚本和需要“打电话回家”才能工作的脚本。我相信通过严格的许可协议来保护您的产品(这会吓唬商业客户购买您的产品,因为被抓到偷窃的风险太高了。)
回答by antmeeks
I wonder if the OP ever found a solution for himself. As far as I know, the only way to work this would be to have all your client accounts on the same server as the scripts you want to include - I've done something similar:
我想知道 OP 是否曾为自己找到解决方案。据我所知,解决这个问题的唯一方法是将所有客户帐户与要包含的脚本放在同一台服务器上 - 我做过类似的事情:
/path_to_myserver_root/httpdocs/clients/client01/wwwroot/scriptA.php /path_to_myserver_root/httpdocs/clients/client02/wwwroot/scriptA.php ETC....
/path_to_myserver_root/httpdocs/clients/client01/wwwroot/scriptA.php /path_to_myserver_root/httpdocs/clients/client02/wwwroot/scriptA.php ETC ....
THEN: /path_to_myserver_root/privatefiles/myapp/scriptB.php
然后:/path_to_myserver_root/privatefiles/myapp/scriptB.php
wwwroot is where each client domain points.
wwwroot 是每个客户端域指向的地方。
scriptA.php has some business logic then includes scriptB.php for it's functions with the full path above:
scriptA.php 有一些业务逻辑,然后包含 scriptB.php 的功能和上面的完整路径:
require('/path_to_myserver_root/privatefiles/myapp/scriptB.php')
require('/path_to_myserver_root/privatefiles/myapp/scriptB.php')
scriptB.php resides in a private protected dir on the server, inaccessible by http, and not traversable by the clients.
scriptB.php 驻留在服务器上的私有保护目录中,http 无法访问,客户端也无法遍历。
Now mind you, my reasons for doing this is to maintain version consistency across multiple accounts, not to withhold some proprietary magical php code from my clientele - But I suppose it could be implemented for that purpose.
现在请注意,我这样做的原因是为了在多个帐户之间保持版本一致性,而不是从我的客户那里扣留一些专有的神奇 php 代码 - 但我想它可以实现这个目的。
Meh, YMMV.
嗯,YMMV。
回答by Martin Milan
When you're trying to go across domains as you have suggested, you're not actually including a file that's sat there ready to do - the process is different. The machine needs to bring back the file over http which isn't what the include statement is all about.
当您按照您的建议尝试跨域时,您实际上并没有包含一个准备就绪的文件 - 过程是不同的。机器需要通过 http 带回文件,这不是 include 语句的全部内容。
Also, if you're on shared hosting, PHP is often configured to prevent you from going outside of your own domain.
此外,如果您使用共享主机,PHP 通常会配置为防止您离开自己的域。
If you aren't under this restriction, one solution might be to use PHP to copy back a copy of the file from the other server, and then include it once it's sat in your domain. Another apporach might be to write a little "deployment" script that copies it everywhere it needs to be whenever you make changes...
如果您不受此限制,一个解决方案可能是使用 PHP 从另一台服务器复制回文件的副本,然后在它位于您的域中后将其包含在内。另一种方法可能是编写一个小“部署”脚本,在您进行更改时将其复制到任何需要的地方......
Hope this helps...
希望这可以帮助...
Martin
马丁
回答by Your Common Sense
rename the first one to .txt
then think twice, are you sure you need cross-domain include
将第一个重命名为 .txt
然后三思而后行,您确定需要跨域包含吗
回答by SoLoGHoST
Use file_get_contents, to open up the file, append it to the second file like so:
使用file_get_contents, 打开文件,将其附加到第二个文件,如下所示:
$secondFile = file_get_contents('http://www.sample.com/includeThis.php');
file_put_contents('your_file', $secondFile, FILE_APPEND);
This will work if you want to put it at the end of your file. Than just do an include on your file.
如果您想将其放在文件末尾,这将起作用。不仅仅是在你的文件中做一个包含。
Anyways, like I said, this is risky and dangerous IMO, especially if you aren't sure about the content it has inside of it.
无论如何,就像我说的,这是有风险和危险的 IMO,特别是如果您不确定其中包含的内容。
Also, your_file will need to be an actual server path, not a URL.
此外, your_file 需要是实际的服务器路径,而不是 URL。

