Contrary to what eng.mrkto.com said, getenv() isn't always case-insensitive. On Linux it is not:
<?php
var_dump(getenv('path')); // bool(false)
var_dump(getenv('Path')); // bool(false)
var_dump(getenv('PATH')); // string(13) "/usr/bin:/bin"
(PHP 4, PHP 5, PHP 7, PHP 8)
getenv — Ortam değişkenlerinden birinin veya tümünün değerini döndürür
Ortam değişkenlerinden birinin veya tümünün değerini döndürür.
Tüm ortam değişkenlerini phpinfo() işlevini kullanarak bir liste halinde görebilirsiniz. Bunların pek çoğunun anlamlarını » RFC 3875 belgesinin özellikle 4.1 "Request Meta-Variables" bölümünde bulabilirsiniz.
değişken
diye bir ortam değişkeni yoksa false
aksi
takdirde belirtilen ortam değişkeninin değerini döndürür.
değişken
null
ise, tüm ortam değişkenlerini içeren
ilişkisel bir dizi döner.
Sürüm: | Açıklama |
---|---|
8.0.0 |
değişken artık null olabiliyor.
|
7.1.0 |
değişken artık atlanabiliyor, bu durumda tüm
ortam değişkenlerini içeren ilişkisel bir dizi dönüyor.
|
7.0.9 |
yerel bağımsız değişkeni eklendi.
|
Örnek 1 - getenv() örneği
<?php
// getenv() kullanım örneği
$ip = getenv('REMOTE_ADDR');
//Veya basitçe bir süper küresel kullan ($_SERVER veya $_ENV)
$ip = $_SERVER['REMOTE_ADDR'];
// Bir ortam değişkeninin değeri, bir SAPI tarafından ayarlanıp
// ayarlanmadığına veya putenv ile değiştirilip değiştirilmediğine
// bakılmaksızın rahatça alınabilir
$ip = getenv('REMOTE_ADDR', true) ?: getenv('REMOTE_ADDR')
?>
PHP, Fast CGI gibi bir SAPI olarak çalışıyorsa, bu işlev daima SAPI
tarafından atanan bir ortam değişkeninin değerini döndürür
(putenv() ile aynı isimde yerel bir ortam değişkeni
tanımlanmış olsa bile). Yerel olarak tanımlanmış ortam değişkenlerini
döndürmek için yerel
bağımsız değişkeni kullanılır.
Contrary to what eng.mrkto.com said, getenv() isn't always case-insensitive. On Linux it is not:
<?php
var_dump(getenv('path')); // bool(false)
var_dump(getenv('Path')); // bool(false)
var_dump(getenv('PATH')); // string(13) "/usr/bin:/bin"
As noted on httpoxy.org, getenv() can confuse you in having you believe that all variables come from a "safe" environment (not all of them do).
In particular, $_SERVER['HTTP_PROXY'] (or its equivalent getenv('HTTP_PROXY')) can be manually set in the HTTP request header, so it should not be considered safe in a CGI environment.
In short, try to avoid using getenv('HTTP_PROXY') without properly filtering it.
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') ?>
I did a benchmark about env.
constants :
0.00067687034606934 ms
getenv :
0.056761026382446 ms
(less is better)
https://github.com/eftec/php-benchmarks#define--const--env
And, in Windows at leat, reading the env value is considerably slow (in comparison with a constant), so PHP doesn't cache the information and asks to the OS the env value per call.
So, if you are calling once per request, then there is not a problem. However, if you are calling it many times per request, then it could affects the performance.
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 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.
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.
It is worth noting that since getenv('MY_VARIABLE') will return false when the variable given is not set, there is no direct way to distinguish between a variable that is unset and one that is explicitly set to the value bool(false) when using getenv().
This makes it somewhat tricky to have boolean environment variables default to true if unset, which you can work around either by using "falsy" values such as 0 with the strict comparison operators or by using the superglobal arrays and isset().
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.
Beware that when using this function with PHP built-in server – i.e. php -S localhost:8000 – it will return boolean FALSE.
From PHP 7.1 => getenv() no longer requires its parameter. If the parameter is omitted, then the current environment variables will be returned as an associative array.
Source: http://php.net/manual/en/migration71.changed-functions.php
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