PHPKonf Istanbul PHP Conference 2019 - Call for Papers

getenv

(PHP 4, PHP 5, PHP 7)

getenv環境変数の値を取得する

説明

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

環境変数の値を取得します。

phpinfo() を使えば、すべての環境変数の一覧を見ることができます。 これらの変数の多くは、 » RFC 3875 の section 4.1, "Request Meta-Variables" に挙げられているものです。

パラメータ

varname

変数の名前。

local_only

true に設定すると、(OS または putenv により設定された) ローカルの環境変数のみを返します。

返り値

varname が示す環境変数の値を返し、 環境変数 varname が存在しない場合はFALSEを返します。 varname が省略された場合、 すべての環境変数は連想配列として返されます。

変更履歴

バージョン 説明
7.1.0 varname は、すべての環境変数の連想配列を取得するために 省略することができるようになりました。
5.5.38, 5.6.24, 7.0.9 local_only パラメーターが追加されました。

注意

警告

PHP が Fast CGI のような SAPI で実行中の場合、 同じ名前のローカル環境変数を設定するために putenv() が使用されても、 この関数は SAPI により設定された環境変数の値を常に返します。 ローカル設定された環境変数の値を返すには、 local_only パラメーターを使用してください。

例1 getenv() の例

<?php
// getenv() の使用例
$ip getenv('REMOTE_ADDR');

// または単純にスーパーグローバル($_SERVER または $_ENV)を使用します
$ip $_SERVER['REMOTE_ADDR'];

// SAPI により設定されたか、または putenv により変更されたかどうかを無視して
// 環境変数の値を安全に取得します。
$ip getenv('REMOTE_ADDR'true) ?: getenv('REMOTE_ADDR')
?>

参考

add a note add a note

User Contributed Notes 13 notes

up
35
eng.mrkto.com
8 years ago
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') ?>
up
5
Anonymous
8 months ago
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"
up
6
yw()beeznest!com
2 years ago
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.
up
2
larby dot robert at gmail dot com
4 months ago
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
up
15
php at keith tyler dot com
6 years ago
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.
up
8
hello at jabran dot me
3 years ago
Beware that when using this function with PHP built-in server – i.e. php -S localhost:8000 – it will return boolean FALSE.
up
2
pritisn at gmail dot com
3 years ago
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.
up
5
kyong
14 years ago
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.
up
2
chuck dot reeves at gmail dot com
8 years ago
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
up
1
sam at sambarrow dot com
10 years ago
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.
up
0
jaraco at jaraco dot com
1 year ago
The example on how to fallback produces a syntax error on PHP 5.2:

-bash-3.2$ cat test.php
<?php

$ip
= getenv('REMOTE_ADDR', true) ?: getenv('REMOTE_ADDR')

?>

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

<br />
<b>Parse error</b>:  syntax error, unexpected ':' in <b>/home/content/25/11223125/test.php</b> on line <b>3</b><br />

On PHP 5.2, one must write

$ip = getenv('REMOTE_ADDR', true) ? getenv('REMOTE_ADDR', true) : getenv('REMOTE_ADDR')
up
-27
f dot hartmann2 at gmx dot net
9 years ago
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
up
-32
renko at <remove>virtual-life dot net
14 years ago
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"].
To Top