Dec 17, 2007

PHP中远程文件的调用-关于Allow url fopen

关键词:Allow url fopen,php

最近发现blog突然出了点小问题,调试后发现是服务器提供商基于安全理由把Allow url fopen选项给关了,而我自己写的有一些代码中就用了相应的功能,所以导致了问题的出现,具体错误特征如下:

URL file-access is disabled in the server configuration

打开Allow url fopen的作用主要是在php程序中可以用http或者ftp方式来调用其他文件。如下面的代码:

<?php include("http://example.com/includes/example_include.php"); ?>

而关闭该选项后,使用上面的代码就会出错,必须换成下面的代码方可。

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/example_include.php"); ?>

这里,系统变量 $_SERVER['DOCUMENT_ROOT']返回的是远程系统根目录。

其实现在很多服务商都把allow url fopen功能给关了,主要原因是防止黑客的注入式进攻,所以强烈建议用第二种方法来写代码。另外,如果还是想用url方式来写代码的话,可以考虑用curl

Post by SUN @ 10:31 pm | 与时俱进 | Add to del.icio.us | Digg This!
(Required)
(Required, will not be published)
:mrgreen: :| :twisted: :arrow: 8O :) :? 8) :evil: :D :idea: :oops: :P :roll: ;) :cry: :o :lol: :x :( :!: :?: