©
本文檔使用 php中文網手册 發布
(PHP 4, PHP 5, PHP 7)
getenv — 获取一个环境变量的值
$varname
)获取一个环境变量的值。
使用 phpinfo() 你可以看到所有环境变量的列表。 这些变量很多都在 » RFC 3875 的范围之内, 尤其是章节4.1,"Request Meta-Variables"。
varname
变量名。
返回环境变量 varname
的值,
如果环境变量 varname
不存在则返回 FALSE
。
Example #1 getenv() 例子
<?php
// getenv() 使用示例
$ip = getenv ( 'REMOTE_ADDR' );
// 或简单仅使用全局变量($_SERVER 或 $_ENV)
$ip = $_SERVER [ 'REMOTE_ADDR' ];
?>
[#1] pritisn at gmail dot com [2015-05-19 11:55:08]
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.
[#2] hello at jabran dot me [2015-04-03 06:47:24]
Beware that when using this function with PHP built-in server ?C i.e. php -S localhost:8000??C it will return boolean FALSE.
[#3] php at keith tyler dot com [2012-02-08 00:51:15]
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.
[#4] eng.mrkto.com [2010-07-29 11:36:18]
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') ?>
[#5] chuck dot reeves at gmail dot com [2010-05-06 11:59:27]
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
[#6] f dot hartmann2 at gmx dot net [2009-07-23 05:42:55]
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
[#7] sam at sambarrow dot com [2008-03-12 17:32:00]
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.
[#8] renko at <remove>virtual-life dot net [2004-11-08 06:40:35]
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"].
[#9] kyong [2004-02-04 01:06:19]
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.