4

我正在尝试包含来自 url 的文件,但是我得到了以下错误。

Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /Applications/MAMP/htdocs/diningtime/testsite/salims/index1.php on line 58

Warning: include(http://localhost/diningtime/templates/100/index.php): failed to open stream: no suitable wrapper could be found in /Applications/MAMP/htdocs/diningtime/testsite/salims/index1.php on line 58

Warning: include(): Failed opening 'http://localhost/diningtime/templates/100/index.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php5.4.4/lib/php') in /Applications/MAMP/htdocs/diningtime/testsite/salims/index1.php on line 58
Test

只是想知道周围是否有任何问题?

我的 PHP 包含代码是

<?php include "$location/index.php"; ?>

感谢您在这方面的帮助。

4

2 回答 2

21

您在包含路径时使用了完整的 URL,它告诉 PHP 尝试执行 HTTP 请求以获取该文件。这不是你这样做的方式。除非...100/index.php输出 PHP 代码,否则您将获得一些 HTML 或任何内容作为包含结果,而不是文件中的 php 代码。请记住 - 您是通过 URL 获取的,这意味着它是一个 HTTP 请求,这意味着网络服务器将执行该脚本并提供其输出,而不仅仅是提供其源代码。

网络服务器无法判断该脚本的 HTTP 请求是来自同一服务器上另一个 PHP 脚本的包含调用。隐藏在俄罗斯的某个黑客想要窃取您的源代码,也可以很容易地请求该脚本。你希望你的源代码像这样对世界可见吗?

对于本地文件,您永远不应该使用完整的 url。它的效率非常低,并且不会做你想做的事。为什么不简单地拥有

include('/path/to/templates/100/index.php');

相反,这将是一个本地文件请求,不包括 HTTP 阶段?

于 2012-11-07T20:22:33.690 回答
1

我有一个类似的问题。

考虑到“Marc B”的帖子,很明显使用绝对 URL 不是一个好主意,即使可以通过将 php.ini 编辑为“ficuscr”状态来实现。我不确定此解决方法是否适用于您的具体情况,因为它仍然需要根据每个页面在文件夹结构中的位置进行调整,但是如果像我一样,您的文件中有很多包含内容,它确实会使事情变得更简单网站。

<?php $root="../";?>
<?php include($root . 'folder-A/file_to_be_included_1.php');?>
<?php include($root . 'folder-A/file_to_be_included_2.php');?>
<?php include($root . 'folder-B/file_to_be_included_3.php');?>
<?php include($root . 'folder-B/file_to_be_included_4.php');?>
<?php include($root . 'folder-B/sub-folder/file_to_be_included_5.php');?>

对我来说,这意味着如果我将页面移动到文件夹结构中的另一个位置,例如在其当前位置的子文件夹中,我需要做的就是修改<?php $root="../";?><?php $root="../../";?>例如

于 2013-09-11T19:15:50.200 回答