百科狗-知识改变命运!
--

getenv() - php 选项信息函数

梵高1年前 (2023-11-21)阅读数 29#技术干货
文章标签环境变量

getenv()

(PHP 4, PHP 5, PHP 7)

获取一个环境变量的值

说明

getenv(string $varname[,bool $local_only=FALSE]): stringgetenv(void): array

获取一个环境变量的值。

使用phpinfo()你可以看到所有环境变量的列表。这些变量很多都在» RFC 3875的范围之内,尤其是章节4.1,"Request Meta-Variables"。

参数

$varname

变量名。

$local_only

getenv() - php 选项信息函数

设置为 true 以仅返回本地环境变量(由操作系统或putenv()设置)。

返回值

返回环境变量$varname的值,如果环境变量$varname不存在则返回FALSE。如果省略$varname,则所有环境变量都将作为关联数组array返回。

更新日志

版本说明
7.1.0现在可以省略$varname来检索所有环境变量的关联数组array。
5.5.38, 5.6.24, 7.0.9添加$local_only参数。

注释

Warning

如果 PHP 在诸如 Fast CGI 之类的 SAPI 中运行,则此函数将始终返回由 SAPI 设置的环境变量的值,即使已使用putenv()来设置同名的本地环境变量。使用$local_only参数返回本地设置的环境变量的值。

范例

Example #1getenv()例子

参见

  • putenv() 设置环境变量的值
  • apache_getenv() 获取 Apache subprocess_env 变量
  • 超全局变量
Contrary to what eng.mrkto.com said, getenv() isn't always case-insensitive. On Linux it is not: 
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
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.
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.
Beware that when using this function with PHP built-in server – i.e. php -S localhost:8000 – it will return boolean FALSE.
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.
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
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.
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().
The example on how to fallback produces a syntax error on PHP 5.2:
-bash-3.2$ cat test.php

-bash-3.2$ /web/cgi-bin/php5 test.php 
Content-type: text/html

Parse error: syntax error, unexpected ':' in /home/content/25/11223125/test.php on line 3
On PHP 5.2, one must write $ip = getenv('REMOTE_ADDR', true) ? getenv('REMOTE_ADDR', true) : getenv('REMOTE_ADDR')
A function returning the remote adress of the visiting browser could look like this:

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
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"].

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)