PHP Doku:: Liefert den Wert einer Umgebungsvariable - function.getenv.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDas Verhalten von PHP beeinflussenPHP-Optionen und -InformationenPHP-Optionen-/-Informationen-Funktionengetenv

Ein Service von Reinhard Neidl - Webprogrammierung.





(PHP 4, PHP 5)

getenvLiefert den Wert einer Umgebungsvariable


string getenv ( string $varname )

Liefert den Wert einer Umgebungsvariable.

Eine Liste aller Umgebungsvariablen können Sie über die Funktion phpinfo() anzeigen lassen. Die Bedeutung vieler dieser Variablen können Sie in der » CGI-Spezifikation im Abschnitt über » Umgebungsvariablen nachlesen.



Der Variablenname


Gibt den Wert der Umgebungsvariable varname zurück oder FALSE bei Fehlern.


Beispiel #1 getenv()-Beispiel

// nutzen Sie getenv() ...
$ip getenv('REMOTE_ADDR');

// ... oder einfache die superglobalen Arrays ($_SERVER oder $_ENV)

Siehe auch

10 BenutzerBeiträge:
- Beiträge aktualisieren...
29.07.2010 20:36
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') ?>
chuck dot reeves at gmail dot com
6.05.2010 20:59
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
23.07.2009 14:42
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.

estarapapax at gmail dot com
19.02.2009 6:19
This is a sample function for checking if your visitor comes from this certain country. This is especially useful for amateur webmasters who don't want to use sql databases.

Sample use of function:
if(check_coutry('') === true)
'You are from the Philippines';

Basically, you'll need CSV (or TXT) which lists the IP ranges of a certain country. Example (excerpt of the philippines.csv only):,,,,,
and so on until the end

You may obtain these CSV files in a number of websites out there. Be sure to update these CSV files regularly as these IP ranges change from time to time.

Here is the function:
function check_country($url_csv){
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url_csv);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$str_fromfile = curl_exec($curl_handle);
$range = explode("\n", $str_fromfile);

$ip_addr = getenv(REMOTE_ADDR); //gets the IP of the visitor
$ip_byte = explode('.', $ip_addr);
$ip_number = (16777216 * (int) $ip_byte[0]) + (65536 * (int) $ip_byte[1]) + (256 * (int) $ip_byte[2]) + ((int) $ip_byte[3]);

$i = 0; $range[$i] != NULL && $is_positive == NULL; $i++){ // the condition $line[$i] != NULL means that you should not put blank lines before the end of your CSV. The values should start at line 1.
$range[$i] = rtrim(ltrim($range[$i])); //you may remove this if you are sure the CSV doesnt contain whitespaces
$ends_addr = explode(',', $range[$i]); //for CSV (comma-separated values), comma is the separator. You may change this if your TXT uses different separator.
$ends_addr[0] = rtrim($ends_addr[0]); //again, you may remove this if your CSV is free from whitespaces
$ends_addr[1] = ltrim($ends_addr[1]); //yet, again
$start_ip_byte = explode('.', $ends_addr[0]);
$end_ip_byte = explode('.', $ends_addr[1]);
$start_ip_number = (16777216 * (int) $start_ip_byte[0]) + (65536 * (int) $start_ip_byte[1]) + (256 * (int) $start_ip_byte[2]) + ((int) $start_ip_byte[3]);
$end_ip_number = (16777216 * (int) $end_ip_byte[0]) + (65536 * (int) $end_ip_byte[1]) + (256 * (int) $end_ip_byte[2]) + ((int) $end_ip_byte[3]);
$ip_number >= $start_ip_number && $ip_number <= $end_ip_number)
$is_positive = 1;
$is_positive == 1)
sam at sambarrow dot com
13.03.2008 2:32
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.
renko at virtual-life dot net
8.11.2004 15:40
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"].
4.02.2004 10:06
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.
daman at SPAM_BlockERmralaska dot com
8.09.2002 15:37
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.
alex at acid-edge dot net
23.07.2002 19:32
Note that some caches seem to send the client-ip header *backwards*. be careful :)
john-php at pc dot xs4all dot nl
16.08.2000 3:56
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.

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)