2010-09-16 40 views
2

我試圖在我的服務器上安裝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',並得到了同樣的錯誤。

回答

2

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"])); 
    } 
} 
1

默認情況下,您需要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的解決方案聽起來是個好主意。

相關問題