getenv

(PHP 4, PHP 5)

getenvRetourne la valeur d'une variable d'environnement

Description

string getenv ( string $varname )

Retourne la valeur d'une variable d'environnement.

Vous pouvez voir une liste compl├Ęte des variables d'environnement en utilisant la fonction phpinfo(). Vous pouvez trouver la signification de chacune d'entre elles en consultant la » RFC 3875, en particulier la section 4.1 "Request Meta-Variables".

Liste de param├Ętres

varname

Le nom de la variable.

Valeurs de retour

Retourne la valeur de la variable d'environnement varname, ou FALSE si la variable d'environnement varname n'existe pas.

Exemples

Exemple #1 Exemple avec getenv()

<?php
// Exemple d'utilisation de getenv()
$ip getenv('REMOTE_ADDR');

// Ou utilisez simplement une Superglobale ($_SERVER ou $_ENV)
$ip $_SERVER['REMOTE_ADDR'];
?>

Voir aussi

add a note add a note

User Contributed Notes 9 notes

up
7
php at keith tyler dot com
2 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.
up
6
eng.mrkto.com
3 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') ?>
up
5
kyong
10 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.
up
3
daman at SPAM_BlockERmralaska dot com
11 years ago
Be careful using HTTP_X_FORWARDED_FOR in conditional statements collecting the IP address. Sometimes the user's LAN address will get forwarded, which of course is pretty worthless by itself.
up
2
f dot hartmann2 at gmx dot net
4 years ago
A function returning the remote adress of the visiting browser could look like this:

<?php
function getIPfromXForwarded() {
   
$ipString=@getenv("HTTP_X_FORWARDED_FOR");
   
$addr = explode(",",$ipString);
    return
$addr[sizeof($addr)-1];
}
?>

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.

Greetings
up
1
john-php at pc dot xs4all dot nl
13 years ago
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.
up
1
sam at sambarrow dot com
6 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.
up
-2
chuck dot reeves at gmail dot com
4 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
up
-4
renko at <remove>virtual-life dot net
9 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