PHP 7.0.0 RC 8 Released


(PHP 4, PHP 5, PHP 7)

getenvObtém uma variável de ambiente


string getenv ( string $varname )

Retorna o nome da variável de ambiente varname, ou FALSE em caso de erro.

getenv("REMOTE_ADDR"); // obtém o número ip do usuário

Você poderá ver uma lista de todas as variáveis de ambiente usando phpinfo(). Você poderá encontram o que muitas delas indicam dando uma olhada em » CGI specification, especialmente em » page on environmental variables.


Esta função não funciona no modo ISAPI.

Veja também putenv().

add a note add a note

User Contributed Notes 9 notes

5 years ago
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') ?>
hello at jabran dot me
7 months ago
Beware that when using this function with PHP built-in server – i.e. php -S localhost:8000 – it will return boolean FALSE.
11 years ago
As you know, getenv('DOCUMENT_ROOT') is useful.
However, under CLI environment(I tend to do quick check
if it works or not), it doesn't work without modified php.ini
file. So I add "export DOCUMENT_ROOT=~" in my .bash_profile.
php at keith tyler dot com
3 years ago
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.
sam at sambarrow dot com
7 years ago
SERVER_NAME is the name defined in the apache configuration.
HTTP_HOST is the host header sent by the client when using the more recent versions of the http protocol.
pritisn at gmail dot com
6 months ago
for quick check of getenv() adding a new env variable -
if you add a new env variable, make sure not only apache but xampp is also restarted.
Otherwise getenv() will return false for the newly added env variable.
chuck dot reeves at gmail dot com
5 years ago
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
f dot hartmann2 at gmx dot net
6 years ago
A function returning the remote adress of the visiting browser could look like this:

function getIPfromXForwarded() {
$addr = explode(",",$ipString);

Note that some adresses are followed by a whitespace and ip2long(getIPfromXForwarded()) would not return the expected result.

Make use of trim() in your scripts, either in the function itself, or the surrounding space of the caller.

renko at <remove>virtual-life dot net
11 years ago
The function 'getenv' does not work if your Server API is ASAPI (IIS).

So, try to don't use getenv('REMOTE_ADDR'), but $_SERVER["REMOTE_ADDR"].
To Top