使用 php 包括来自另一台服务器的 php 文件
我有两个 PHP 文件位于不同的服务器上,一个位于 http://www.mysite.com/main.php
,另一个位于 http://www.sample.com/includeThis.php
.
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
.
我想包含第一个文件中的第二个文件.
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";
还有第一个文件:
<?php
include "http://www.sample.com/includeThis.php";
echo $foo;
有什么办法可以做到吗?
Is there any way I can do this?
推荐答案
不,此设置在大多数 Web 服务器 (php.ini) 中默认禁用/不允许,因此您不能使用 include
出于安全原因,包含来自远程地址的文件.
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.
如果您仍想允许包含远程文件,指令 allow_url_include
必须在 php.ini 中设置为 On
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)
如果你想读取远程文件的内容,你可以使用file_get_contents
函数代替 BUT 这将作为纯 HTML 标记代码返回,有不会是任何服务器端代码.
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.
相关文章