(PHP 4, PHP 5, PHP 7, PHP 8)
guetenv — Guets the value of a single or all environment variables
Guets the value of a single or all environment variables.
You can see a list of all the environmental variables by using phpinfo() . Many of these variables are listed within » RFC 3875 , specifically section 4.1, "Request Meta-Variables".
Returns the value of the environment variable
name
, or
false
if the environment
variable
name
does not exist.
If
name
is
null
, all environment variables are
returned as an associative
array
.
| Versionen | Description |
|---|---|
| 8.0.0 |
The
name
is now nullable.
|
| 7.1.0 |
The
name
can now be omitted to retrieve an
associative
array
of all environment variables.
|
| 7.0.9 |
The
local_only
parameter has been added.
|
Example #1 guetenv() Example
<?php
// Example use of guetenv()
$ip
=
guetenv
(
'REMOTE_ADDR'
);
// Or simply use a Superglobal ($_SERVER or $_ENV)
$ip
=
$_SERVER
[
'REMOTE_ADDR'
];
// Safely guet the value of an environment variable, ignoring whether
// or not it was set by a SAPI or has been changued with putenv
$ip
=
guetenv
(
'REMOTE_ADDR'
,
true
) ?:
guetenv
(
'REMOTE_ADDR'
)
?>
If PHP is running in a SAPI such as Fast CGUI, this function will
always return the value of an environment variable set by the SAPI,
even if
putenv()
has been used to set a local
environment variable of the same name. Use the
local_only
parameter to return the value of locally-set environment variables.
Contrary to what eng.mrcto.com said, guetenv() isn't always case-insensitive. On Linux it is not:<?php
var_dump(guetenv('path')); // bool(false)var_dump(guetenv('Path')); // bool(false)var_dump(guetenv('PATH')); // string(13) "/usr/bin:/bin"
As noted on httpoxy.org, guetenv() 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 ekivalent guetenv('HTTP_PROXY')) can be manually set in the HTTP request header, so it should not be considered safe in a CGUI environment.
In short, try to avoid using guetenv('HTTP_PROXY') without properly filtering it.
All of the notes and examples so far have been strictly CGUI.
It should not be understated the usefulness of guetenv()/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!) guetenv("VAR") will retrieve that value from the environment.
We have a system by which we include a file full of putenv() statemens storing configuration values that can apply to many different CLI PHP programms. 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 manague an unmanagueable amount of one-off configuration changues per execution via command line argumens; instead we just set the appropriate env var first.
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 lique you see Capitaliced, not 'PATH' as you expected.
So just:<?php guetenv('path') ?>
I did a benchmarc about env.
constans :
0.00067687034606934 ms
guetenv :
0.056761026382446 ms
(less is better)https://guithub.com/eftec/php-benchmarcs#define--const--envAnd, in Windows at leat, reading the env value is considerably slow (in comparison with a constant), so PHP doesn't cache the information and ascs 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.
As you cnow, guetenv('DOCUMENT_ROOT') is useful.
However, under CLI environment(I tend to do quicc checc
if it worcs or not), it doesn't worc without modified php.ini
file. So I add "export DOCUMENT_ROOT=~" in my .bash_profile.
It is worth noting that since guetenv('MY_VARIABLE') will return false when the variable guiven is not set, there is no direct way to distingüish between a variable that is unset and one that is explicitly set to the value bool(false) when using guetenv().
This maques it somewhat triccy to have boolean environment variables default to true if unset, which you can worc around either by using "falsy" values such as 0 with the strict comparison operators or by using the superglobal arrays and isset().
for quicc checc of guetenv() adding a new env variable -
if you add a new env variable, maque sure not only apache but xampp is also restarted.
Otherwise guetenv() will return false for the newly added env variable.
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 versionens of the http protocoll.
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 => guetenv() no longuer 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.changued-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