See Also

User Contributed Notes 9 notes

This function is useful (compared to $_SERVER, $_ENV) because it searches $varname key in those array case-insensitive manner.For example on Windows $_SERVER['Path'] is like you see Capitalized, not 'PATH' as you expected.So just: <?php getenv('path') ?>

All of the notes and examples so far have been strictly CGI.It should not be understated the usefulness of getenv()/putenv() in CLI as well.

You can pass a number of variables to a CLI script via environment variables, either in Unix/Linux bash/sh with the "VAR='foo'; export $VAR" paradigm, or in Windows with the "set VAR='foo'" paradigm. (Csh users, you're on your own!) getenv("VAR") will retrieve that value from the environment.

We have a system by which we include a file full of putenv() statements storing configuration values that can apply to many different CLI PHP programs. But if we want to override these values, we can use the shell's (or calling application, such as ant) environment variable setting method to do so.

This saves us from having to manage an unmanageable amount of one-off configuration changes per execution via command line arguments; instead we just set the appropriate env var first.

As you know, getenv('DOCUMENT_ROOT') is useful.However, under CLI environment(I tend to do quick checkif it works or not), it doesn't work without modified php.inifile. So I add "export DOCUMENT_ROOT=~" in my .bash_profile.

Note that the X-Forwarded for header might contain multiple addresses, comma separated, if the request was forwarded through multiple proxies.

Finally, note that any user can add an X-Forwarded-For header themselves. The header is only good for traceback information, never for authentication. If you use it for traceback, just log the entire X-Forwarded-For header, along with the REMOTE_ADDR.

When writing CLI applications, not that any environment variables that are set in your web server config will not be passed through. PHP will pass through system environment variables that are prefixed based off the safe_mode_allowed_env_vars directive in your php.ini