putenv

(PHP 4, PHP 5, PHP 7)

putenvSets the value of an environment variable

설명

bool putenv ( string $setting )

Adds setting to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state.

Setting certain environment variables may be a potential security breach. The safe_mode_allowed_env_vars directive contains a comma-delimited list of prefixes. In Safe Mode, the user may only alter environment variables whose names begin with the prefixes supplied by this directive. By default, users will only be able to set environment variables that begin with PHP_ (e.g. PHP_FOO=BAR). Note: if this directive is empty, PHP will let the user modify ANY environment variable!

The safe_mode_protected_env_vars directive contains a comma-delimited list of environment variables, that the end user won't be able to change using putenv(). These variables will be protected even if safe_mode_allowed_env_vars is set to allow to change them.

인수

setting

The setting, like "FOO=BAR"

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Setting an environment variable

<?php
putenv
("UNIQID=$uniqid");
?>

주의

Warning

The safe_mode_allowed_env_vars and safe_mode_protected_env_vars directives only take effect when safe_mode is enabled.

참고

  • getenv() - 환경 변수 값을 얻습니다
  • apache_setenv() - 아파치의 서브프로세스의 환경 변수를 설정합니다

add a note add a note

User Contributed Notes 7 notes

up
58
php at keith tyler dot com
13 years ago
putenv/getenv, $_ENV, and phpinfo(INFO_ENVIRONMENT) are three completely distinct environment stores. doing putenv("x=y") does not affect $_ENV; but also doing $_ENV["x"]="y" likewise does not affect getenv("x"). And neither affect what is returned in phpinfo().

Assuming the USER environment variable is defined as "dave" before running the following:

<?php
print "env is: ".$_ENV["USER"]."\n";
print
"(doing: putenv fred)\n";
putenv("USER=fred");
print
"env is: ".$_ENV["USER"]."\n";
print
"getenv is: ".getenv("USER")."\n";
print
"(doing: set _env barney)\n";
$_ENV["USER"]="barney";
print
"getenv is: ".getenv("USER")."\n";
print
"env is: ".$_ENV["USER"]."\n";
phpinfo(INFO_ENVIRONMENT);
?>

prints:

env is: dave
(doing: putenv fred)
env is: dave
getenv is: fred
(doing: set _env barney)
getenv is: fred
env is: barney
phpinfo()

Environment

Variable => Value
...
USER => dave
...
up
8
t7to7
4 years ago
White spaces are allowed in environment variable names so :

<?php
putenv
('U =33');
?>

Is not equivalent to

<?php
putenv
('U=33');
?>
up
26
JM
17 years ago
The other problem with the code from av01 at bugfix dot cc is that
the behaviour is as per the comments here, not there:
<?php
putenv
('MYVAR='); // set MYVAR to an empty value.  It is in the environment
putenv('MYVAR'); // unset MYVAR.  It is removed from the environment
?>
up
6
Anonymous Coder
11 years ago
It's the putenv() type of environment variables that get passed to a child process executed via exec().

If you need to delete an existing environment variable so the child process does not see it, use:

putenv('FOOBAR');

That is, leave out both the "=" and a value.
up
9
david dot boyce at messagingdirect dot comnospam
23 years ago
Environment variables are part of the underlying operating system's
way of doing things, and are used to pass information between a parent
process and its child, as well as to affect the way some internal
functions behave.  They should not be regarded as ordinary PHP
variables.

A primary purpose of setting environment variables in a PHP script is
so that they are available to processes invoked by that script using
e.g. the system() function, and it's unlikely that they would need to
be changed for other reasons.

For example, if a particular system command required a special value
of the environment variable LD_LIBRARY_PATH to execute successfully,
then the following code might be used on a *NIX system:

<?php
$saved
= getenv("LD_LIBRARY_PATH");        // save old value
$newld = "/extra/library/dir:/another/path/to/lib"// extra paths to add
if ($saved) { $newld .= ":$saved"; }           // append old paths if any
putenv("LD_LIBRARY_PATH=$newld");        // set new value
system("mycommand -with args");        // do system command;
                        // mycommand is loaded using
                        // libs in the new path list
putenv("LD_LIBRARY_PATH=$saved");        // restore old value
?>

It will usually be appropriate to restore the old value after use;
LD_LIBRARY_PATH is a particularly good example of a variable which it
is important to restore immediately, as it is used by internal
functions.

If php.ini configuration allows, the values of environment variables
are made available as PHP global variables on entry to a script, but
these global variables are merely copies and do not track the actual
environment variables once the script is entered.  Changing
$REMOTE_ADDR (or even $HTTP_ENV_VARS["REMOTE_ADDR"]) should not be
expected to affect the actual environment variable; this is why
putenv() is needed.

Finally, do not rely on environment variables maintaining the same
value from one script invocation to the next, especially if you have
used putenv().  The result depends on many factors, such as CGI vs
apache module, and the exact way in which the environment is
manipulated before entering the script.
up
-18
broussardrobert at sbcglobal dot net
6 years ago
Great examples for the trivial case that most can figure out directly from the manual, but where is the trivially more complex example describing how to set multiple variables?  I tried separating with spaces, commas, semicolons, multiple invocations of setenv, all to no avail.  Please try to include trivial extensions to the examples...
up
-18
broussardrobert at sbcglobal dot net
6 years ago
Multiple invocations of putenv() work as expected: the real problem was that some of the putenv() invocations in my script contained typographical errors.

I typed, e.g., putenv( "IMAGE_DATABASE=" . $_SERVER{'IMAGE_DATABASE'} );
which of course is incorrect.  '{' and '}' should have been '[' and ']'.
Because my first call to putenv() did not have that typo, it worked correctly, but the remaining three calls I coded with the typo merely cleared the corresponding environment variables and thus did not make it into the external script I invoked via a system() call.  I suspect the fact my first invocation was typed correctly affected my efforts looking at the syntax in the following invocations.

The trivial extension to the example provided is to merely call putenv() multiple times, once for each variable.
To Top