PHP的问题 - 设置时区时的安全模式错误

问题描述:

我试图在我的服务器上安装PHP application。但我得到这个错误:PHP的问题 - 设置时区时的安全模式错误

Warning: putenv() [function.putenv]: Safe Mode warning: Cannot set environment variable 'PHP_TZ' - it's not in the allowed list in .../public/timezone.inc on line 14

我找到了有问题的文件和代码段(下面)。我将如何修复此代码? PHP_TZ应该做什么?为什么PHP不喜欢它?我能做些什么呢?

//set the timezone 
if ($configdata["timezone"] != "") { 
    putenv("PHP_TZ=" . stripslashes($configdata["timezone"])); 
    putenv("TZ=" . stripslashes($configdata["timezone"])); 

    //for >= PHP 5.1 
    if(function_exists("date_default_timezone_set")) { 
     date_default_timezone_set($configdata["timezone"]); 
    } 

我在PHP 5.2.10上。我为$ configdata [“timezone”]的值尝试了'欧洲/苏黎世'和'UTC',并得到了同样的错误。

PHP_TZ代表PHP时区。

从版本5.1开始,您需要通过date_default_timezone_set函数或通过PHP配置来设置它。从文档:

Note:

Since PHP 5.1.0 (when the date/time functions were rewritten), every call to a date/time function will generate a E_NOTICE if the timezone isn't valid, and/or a E_WARNING message if using the system settings or the TZ environment variable.

您可以做的最简单的修复方法如下。

//set the timezone 
if ($configdata["timezone"] != "") { 
    //for >= PHP 5.1 
    if(function_exists("date_default_timezone_set")) { 
     date_default_timezone_set($configdata["timezone"]); 
    // for PHP < 5.1 
    } else { 
     putenv("PHP_TZ=" . stripslashes($configdata["timezone"])); 
     putenv("TZ=" . stripslashes($configdata["timezone"])); 
    } 
} 

默认情况下,您需要SHOULD能够设置它。见下面的粗体部分。看来您的托管服务提供商通过safe_mode_protected_env_vars将其禁用。

bool putenv (string $setting )

Adds setting to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state.

Setting certain environment variables may be a potential security breach. The safe_mode_allowed_env_vars directive contains a comma-delimited list of prefixes. In Safe Mode, the user may only alter environment variables whose names begin with the prefixes supplied by this directive. By default, users will only be able to set environment variables that begin with PHP_ (e.g. PHP_FOO=BAR). Note: if this directive is empty, PHP will let the user modify ANY environment variable!

The safe_mode_protected_env_vars directive contains a comma-delimited list of environment variables, that the end user won't be able to change using putenv(). These variables will be protected even if safe_mode_allowed_env_vars is set to allow to change them.

HoLyVieR的解决方案听起来是个好主意。