PHP 网址包含不起作用


PHP URL include not working

我对URL包含有问题,我不明白...:为了进行测试,我编写了以下脚本:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
include("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text"; 
?>

Allow_url_include设置为开。(通过PHP.ini)

Allor_url_fopen ist 设置为打开。(通过PHP.ini)

includetest.php 仅包含用于测试的纯文本。没有 php 代码。

该脚本的结果只是"第一个文本"。之后,脚本将停止。

如果我在包含后使用"或死亡('不起作用');",结果是整个文本(也是第二个文本),并带有以下警告:

警告:include(1):无法打开流:没有这样的文件或目录 在/srv2/www/htdocs/xhtml-test/_baustelle/testphp02 中.php第 6 行 警告:include():打开"1"以包含失败 (include_path='.:/usr/share/php:/usr/share/pear') in /srv2/www/htdocs/xhtml-test/_baustelle/testphp02.php 在第 6 行

为什么?我不知所措...

这是代码的问题:

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

参考资料在这里

您包含的文件不是有效的 php 文件,因为它已被服务器作为 php 保存。

此代码应按预期工作:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
echo file_get_contents("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text"; 
?>

你应该在 PHP include 函数中使用相对路径。

include '/path/to/file.php';  // You can include file by relative path

根据文档,

通过 HTTP 包含

如果在 PHP 中启用了"URL 包含包装器",则可以指定文件 使用 URL 包含在内(通过 HTTP 或其他受支持的包装器 - 请参阅 协议列表支持的协议和包装器)而不是 本地路径名。如果目标服务器将目标文件解释为 PHP 代码,变量可以使用 URL 请求传递到包含的文件 与 HTTP GET 一起使用的字符串。严格来说,这并不相同 包括文件并让它继承父文件的 可变范围;脚本实际上正在远程服务器上运行 然后将结果包含在本地脚本中。

/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */
// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';
$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

警告

安全警告

远程文件可以在远程服务器上处理(取决于 文件扩展名和远程服务器是否运行PHP的事实)但是 它仍然必须生成一个有效的 PHP 脚本,因为它将是 在本地服务器上处理。如果文件来自远程服务器 应该在那里处理并仅输出,readfile() 很多 更好的功能使用。否则,应特别注意 保护远程脚本以生成有效且所需的代码。

这是对路径的理解。

1) 相对路径

index.html
/graphics/image.png
/help/articles/how-do-i-set-up-a-webpage.html

2) 绝对路径

http://www.mysite1.com
http://www.mysite2.com/graphics/image.png
http://www.mysite3.com/help/articles/how-do-i-set-up-a-webpage.html

您会注意到两种不同类型的链接之间的第一个区别是,绝对路径始终包含网站的域名,包括 http://www.,而相对链接仅指向文件或文件路径。当用户单击相对链接时,浏览器会将他们带到当前网站上的该位置。

因此,您只能在链接到网站中的页面或文件时使用相对链接,如果您要链接到其他网站上的某个位置,则必须使用绝对链接

有关更多信息,另请参阅此链接。

希望它能帮助你:)