PHP URL包含不起作用

时间:2016-02-23 09:27:41

标签: php php-include

我的网址包含问题,我不明白......: 为了测试,我编写了以下脚本:

<?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设置为on。 (通过php.ini)

Allor_url_fopen设置为on。 (通过php.ini)

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

该脚本的结果只是&#34;第一个文本&#34;。之后脚本停止了。

如果我使用&#34;或者死亡(&#39;不工作&#39;);&#34;在include之后,结果是整个文本(也是第二个文本),并带有以下警告:

  

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

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

2 个答案:

答案 0 :(得分:0)

以下是代码问题:

// 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';

参考。这个Is Here

您所包含的文件不是有效的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"; 
?>

答案 1 :(得分:0)

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

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

根据文件,

通过HTTP包含

  

如果在PHP中启用了“URL include wrappers”,则可以指定该文件   使用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。,而相对链接仅指向到文件或文件路径。当用户单击相对链接时,浏览器会将其带到当前站点上的该位置。

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

有关详情,请参阅此link

希望它会对你有所帮助:)。