Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: putenv - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

restore_include_path" width="11" height="7"/> <phpversion
Last updated: Fri, 05 Sep 2008

view this page in

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 を返します。

例1 環境変数の設定

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

注意

警告

これらのディレクティブは、 セーフモード が有効な場合にのみ効果があります!

参考



restore_include_path" width="11" height="7"/> <phpversion
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
putenv
JM
17-Feb-2007 11:41
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
05-Jun-2006 09:18
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
03-Oct-2005 11:32
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
08-Feb-2005 07:06
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
28-Mar-2003 03:18
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
08-Mar-2001 08:19
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
14-Sep-2000 02:23
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.

restore_include_path" width="11" height="7"/> <phpversion
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites