php 绕过 allow_url_include=0 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13277460/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:05:41  来源:igfitidea点击:

get around allow_url_include=0 error

php

提问by AppleTattooGuy

I am trying to include a file from a url, however I get the following error thrown up.

我正在尝试从 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

Just wondering if there is anyway around this?

只是想知道是否有任何办法?

My PHP include code is

我的 PHP 包含代码是

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

I appreciate your help on this.

我很感激你在这方面的帮助。

回答by Marc B

You're using a full URL as you include path, which tells PHP to attempt to do an HTTP request to fetch that file. This is NOThow you do this. Unless that ...100/index.phpoutputs PHP code, you are going to get some HTML or whatever as the include result, NOT the php code in the file. Remember - you're fetching via URL, which means it's an HTTP request, which means the webserver will EXECUTE that script and deliver its output, not simply serve up its source code.

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

There's no way for the webserver to tell that the HTTP request for that script is an include call from another PHP script on the same server. It could just as easily be a request for that script from some hacker hiding in Russia wanting to steal your source code. Do you want your source code visible to the world like this?

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

For local files, you should never use a full-blown url. it's hideously inefficient, and will no do what you want. why not simply have

对于本地文件,永远不要使用完整的 url。这是非常低效的,不会做你想做的事。为什么不简单地拥有

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

instead, which will be a local file-only request, with no HTTP stage included?

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

回答by Sam Longman

I have had a similar issue.

我有一个类似的问题。

Considering 'Marc B's' post, it is clear that using absolute URLs is not a great idea, even if it is possible by editing the php.ini as 'ficuscr' states. I'm not sure this workaround will work for your specific situation as it still requires adjustments to each page depending on where it is in your folder structure, but it does makes things simpler if, like me, you have a lot of includes in your website.

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

<?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');?>

For me, this means if I move a page to another location in the folder structure, for example in a sub-folder of its current location, all I need to do is amend <?php $root="../";?>to become <?php $root="../../";?>for example

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