如何防止使用.htaccess或自定义php.ini覆盖设置

Leo*_*Sam 6 php apache security .htaccess

如何确保用户不能使用.htaccess或自定义php.ini覆盖任何我应该使用的配置提示

meg*_*lop 5

要防止用户在.htaccess文件中设置PHP配置值,请不要AllowOverride Options为其虚拟主机授予权限.

或者,将PHP安装为CGI而不是Apache模块,因为CGI版本不受.htaccess文件的影响.但是,从PHP 5.3.0开始,PHP CGI确实解析了每个目录的php.ini文件.我不知道一种方法可以解决这个问题.

编辑:我刚刚在最新的默认php.ini中看到过这个:

; Directives following the section heading [PATH=/www/mysite] only
; apply to PHP files in the /www/mysite directory.  Directives
; following the section heading [HOST=www.example.com] only apply to
; PHP files served from www.example.com.  Directives set in these
; special sections cannot be overridden by user-defined INI files or
; at runtime. Currently, [PATH=] and [HOST=] sections only work under
; CGI/FastCGI.
; http://php.net/ini.sections
Run Code Online (Sandbox Code Playgroud)

因此,如果您在主目录标题下的主php.ini中放置指令,则无法覆盖它们.但是,缺点是您必须为每个虚拟主机执行此操作,因此它将在有许多或经常添加新的虚拟主机的环境中成为PITA.

再次编辑:

进一步阅读揭示了这一点:

; Name for user-defined php.ini (.htaccess) files. Default is ".user.ini"
;user_ini.filename = ".user.ini"
; To disable this feature set this option to empty value
;user_ini.filename =
Run Code Online (Sandbox Code Playgroud)

因此,只需取消注释最后一行即可禁用每个用户的ini文件.:-)