警告:require_once(): http:// 包装器在服务器配置中被 allow_url_include=0 禁用

2022-08-30 09:23:46

我正在尝试通过以下方式在页面中包含php文件

  require_once(http://localhost/web/a.php)

我收到错误

 Warning: require_once(): http:// wrapper is disabled in the server configuration by   allow_url_include=0

我在php中更改.ini这有效,但我认为不是每个人都会让我更改他们的php.ini文件。allow_url_include=1

那么,有没有办法做到这一点呢?


答案 1

生成此警告的原因是您正在为要包含的文件使用完整 URL。这不是正确的方法,因为这样您将从Web服务器获得一些HTML。用:

require_once('../web/a.php');

因此,Web服务器可以执行脚本并交付其输出,而不仅仅是提供源代码(您当前的情况会导致警告)。


答案 2

当我尝试在我的Wordpress主题中包含PHP文件时,我遇到了同样的错误。我能够通过使用引用文件名来绕过它。我无法使用相对路径,因为我的文件将包含在整个主题的不同位置,因此类似的东西将不起作用。dirname(__FILE__)require_once '../path-to/my-file'

替换为确实做到了。require_once get_template_directory_uri() . '/path-to/my-file'require_once dirname( __FILE__ ) . '/path-to/my-file'


推荐