Environmental Variables in PHP: Understanding SetEnv, $_ENV, and $_SERVER
In Apache, the environment variable FOO is set using the SetEnv directive in the .htaccess file. However, PHP can display this variable in either $_ENV or $_SERVER. Why does this occur, and what causes the complex entries in $_SERVER?
Empty $_ENV Variable
By default, PHP's $_ENV variable remains empty unless the variables_order directive in php.ini includes E. This is not commonly enabled in production environments due to performance concerns. To resolve this, set variables_order to EGPCS, which makes $_ENV accessible.
SetEnv in $_SERVER
Confusion arises when using SetEnv in .htaccess, as it places the variable in $_SERVER, not $_ENV. This behavior is not intuitive and may cause misunderstandings.
Entries in $_SERVER
SetEnv can result in duplicate entries in $_SERVER, such as [REDIRECT_FOO] and [FOO]. This occurs because SetEnv creates a regular environment variable (FOO) and a redirect alias (REDIRECT_FOO`).
getenv Function Alternative
The getenv function is always available for retrieving environment variables, regardless of the PHP settings for $_ENV. It is recommended to use getenv if you encounter issues with $_ENV.
Additional Notes
The above is the detailed content of Why are SetEnv variables accessible in both $_ENV and $_SERVER in PHP?. For more information, please follow other related articles on the PHP Chinese website!