一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

PHP中的安全模式是指的什么?

时间:2008-03-13 编辑:简简单单 来源:一聚教程网

手册如是说:
章 24. 安全模式
目录
被安全模式限制或屏蔽的函数
php 的安全模式是为了试图解决共享服务器(shared-server)安全问题而设立的。在结构上,试图在 php 层上解决这个问题是不合理的,但修改 web 服务器层和操作系统层显得非常不现实。因此许多人,非凡是 isp,目前使用安全模式。


表格 24-1. 控制安全模式的设置选项有:

设置选项 默认值
safe_mode off
safe_mode_gid 0
safe_mode_include_dir ""
safe_mode_exec_dir 1
open_basedir ""
safe_mode_allowed_env_vars php_
safe_mode_protected_env_vars ld_library_path
disable_functions ""


当 safe_mode 设置为 on,php 将检查当前脚本的拥有者是否和将被文件函数操作的文件的拥有者相匹配。例如: -rw-rw-r-- 1 rasmus rasmus 33 jul 1 19:20 script.php
-rw-r--r-- 1 root root 1116 may 26 18:01 /etc/passwd

运行 script.php readfile('/etc/passwd');
?>

假如安全模式被激活,则将会导致以下错误: warning: safe mode restriction in effect. the script whose uid is 500 is not
allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2



同时,或许会存在这样的环境,在该环境下,宽松的 gid 检查已经足够,但严格的 uid 检查反而是不适合的。您可以用 safe_mode_gid 选项来控制这种检查。假如设置为 on 则进行宽松的 gid 检查;设置为 off(默认值)则进行 uid 检查。

除了 safe_mode 以外,假如您设置了 open_basedir 选项,则所有的文件操作将被限制在您指定的目录下。例如:
php_admin_value open_basedir /docroot


假如您在设置了 open_basedir 选项后运行同样的 script.php,则其结果会是: warning: open_basedir restriction in effect. file is in wrong directory in
/docroot/script.php on line 2



您也可以单独地屏蔽某些函数。请注重 disable_functions 选项不能在 php.ini 文件外部使用,也就是说您无法在 httpd.conf 文件的按不同虚拟主机或不同目录的方式来屏蔽函数。 假如我们将如下内容加入到 php.ini 文件: disable_functions readfile,system

则我们会得到如下的输出: warning: readfile() has been disabled for security reasons in

热门栏目