PHP 选项/信息 函数
在线手册:中文  英文

putenv

(PHP 4, PHP 5)

putenv设置环境变量的值

说明

bool putenv ( string $setting )

添加 setting 到服务器环境变量。 环境变量仅存活于当前请求期间。 在请求结束时环境会恢复到初始状态。

设置特定的环境变量也有可能是一个潜在的安全漏洞。 safe_mode_allowed_env_vars 包含了一个以逗号分隔的前缀列表。 在安全模式下,用户可以仅能修改用该指令设定的前缀名称的指令。 默认情况下,用户仅能够修改以 PHP_ 开头的环境变量(例如 PHP_FOO=BAR)。 注意:如果此指令是空的,PHP允许用户设定任意环境变量!

safe_mode_protected_env_vars 指令包含了逗号分隔的环境变量列表,使用户最终无法通过 putenv() 修改。 即使 safe_mode_allowed_env_vars 设置允许修改,这些变量也会被保护。

参数

setting

设置,例如 "FOO=BAR"

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 设置一个环境变量

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

注释

Warning

The safe_mode_allowed_env_varssafe_mode_protected_env_vars 指令仅仅在启用安全模式时有效。

参见


PHP 选项/信息 函数
在线手册:中文  英文

用户评论:

Anonymous Coder (2012-03-27 18:28:07)

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.

simon at systemparadox dot co dot uk (2011-02-24 05:23:00)

It seems that putenv() doesn't work when using virtual() to call CGI's. The environment variables don't carry over to the CGI. Try apache_setenv() instead.

php at keith tyler dot com (2010-06-21 17:52:25)

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

JM (2007-02-16 18:41:21)

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
?>

povaddict (2006-06-04 17:18:40)

av01 at bugfix dot cc:
"putenv('MYVAR='); // unset, otherwise this will pass when run the next time"
it won't pass when run next time, "At the end of the request the environment is restored to its original state."

av01 at bugfix dot cc (2005-10-03 07:32:06)

Please be aware, that using putenv() does NOT effect the superglobal $_ENV[] variable. If you want to, set it seperately

<?php
  putenv
('MYVAR=hello');
  
assert(getenv('MYVAR') == 'hello'); // passes 
  
assert($_ENV['MYVAR'] == 'hello'); // fails!
  
putenv('MYVAR='); // unset, otherwise this will pass when run the next time
?>

Iavor (2005-02-07 14:06:58)

Compare to apache_setenv() and apache_getenv().
I had a case setting an env var in VirtualHost which I tried to change with putenv() - but did not work.
apache_setenv() worked.

cap at capsi dot com (2003-03-27 22:18:30)

I've been using putenv with PHP 4.3.1 and Apache 2.0.44, but it does not seem to restore variables correctly. I'm getting +0100 and -0800 entries all across my Apache logs. Manually adding a putenv in page footers to restore the original value seems to fix things, but I still wish I could set the time zone for a specific request only.
I'm not sure whether using putenv affects all threads within the process, that could be another problem.

verkoop at it-design dot com (2001-03-08 03:19:14)

for those who have problems with the putenv ('TZ=Europe/Amsterdam').
I found that there is a solution/work-a-round. It will work, but only if you add  mktime(0,0,0,1,1,1970) on the next line. So:

<?php
 putenv 
('TZ=Europe/Amsterdam');
 
mktime(0,0,0,1,1,1970)
 echo 
date("H:i:s");
?>

david dot boyce at messagingdirect dot comnospam (2000-09-13 22:23:13)

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.

易百教程