在 .htaccess 中检测对 php_value / php_flag 的支持以抑制错误 - PHP CGI 模式 - mod_php

Detect support for php_value / php_flag in .htaccess to suppress errors - PHP CGI mode - mod_php

我在 .htaccess 中使用 php_valuephp_flag 规则,例如:

php_value upload_max_filesize 100M

但是当服务器 运行 处于 CGI 模式而不是 Apache 模式时,这会导致错误,而我必须使用 php.ini 规则,例如:

upload_max_filesize = 100M

我的代码库需要在任一模式下工作,无需针对每个服务器进行手动更改。

我花了一段时间才找到一个解决方案,让这两种方法都到位,因为很难找到,所以我想 post 我的解决方案在这里。

您可以在 .htaccess 中检测 mod_php 并确保仅在支持时尝试使用这些指令。在 .htaccess 中:

<IfModule mod_php5.c>
   php_value upload_max_filesize 100M
   php_flag ...
</IfModule>

注意:根据您的设置,您可能需要将 mod_php5.c 更改为 mod_php4.cmod_php.c

此外,您可以在 php 模式下使用 .htaccess 中的以下内容手动阻止冗余 php.ini 文件的公开访问:

<Files ~ "\.ini">
   Order allow,deny
   Deny from all
</Files>

希望这能为您节省一些时间!