putenv

(PHP 3, PHP 4, PHP 5)

putenv -- Sets the value of an environment variable

Description

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.

警告

These directives have only effect when safe-mode itself is enabled!

例子 1. Setting an environment variable

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

See also getenv().


add a note add a note User Contributed Notes
povaddict
05-Jun-2006 08: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 10: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
?>
EricZ <ezsomething at notthere dot com>
22-Sep-2005 04:29
I posted some code to help load this file, but it got pruned
out by the folks monitoring php.net.  Anyway, the format can
be loaded from third column of '/usr/share/zoneinfo/zone.tab',
available on every Linux platform I've logged into thus far.
For NYC, it looks like this: "America/New_York". There are
also generic CST/PST but then you have to be careful when
daylight savings switches.
EricZ <ezsomethign at notthere dot com>
22-Sep-2005 04:02
Okay, so here's the format that TZ is looking for
"America/New_York".  You can get all of the timezones
from a single file on most unix systems.  This code works
on all Linux systems that I have seen (various redhat/deb)
where you can find this file...  And now you have a nice,
one level nested timezone list by country.

<?php
    
// -- first part, gather all of the zone data
    
$zoneSource = '/usr/share/zoneinfo/zone.tab';
    
$zoneHandle = fopen($zoneSource, "r");
     if (!
$zoneHandle) return NULL;                  //bad file, abort now
    
while (!feof($zoneHandle)) {
      
$zoneLine = ltrim(fgets($zoneHandle, 4096));
       if (
$zoneLine[0]=='#')  continue;          //skip comments
         //Columns...
         // 1.  ISO 3166 2-character country code.
         // 2.  Latitude and longitude of the zone's principal location
         // 3.  Zone name used in value of TZ environment variable.
         // 4.  Comments; present if and only if country has multiple rows.
      
$zoneParts = explode("\t",$zoneLine);      //grab parts
      
if (count($zoneParts) < 3) continue;      //erroneous line!
      
$nameParts = explode('/', $zoneParts[2], 2);  //grab country/name
      
$zoneKey = $nameParts[0];                  //country or area

      
$insertArray = &$zoneNames;                //where to insert?
      
if (count($nameParts) > 1) {                //area/populous
        
if (!isset($zoneNames[$zoneKey]))        //not set before
          
$zoneNames[$zoneKey] = array();
        
$insertArray = &$zoneNames[$zoneKey];    //grab sub (country)
        
$zoneKey = trim($nameParts[1]);          //grab correct key
      
}
      
$zoneKey = preg_replace('/[_]/',' ', $zoneKey);
      
$insertArray[$zoneKey] = trim($zoneParts[2]); //actually set data
    
}                                            //end while not eof
    
fclose($zoneHandle);
?>
Eric Z <ezsomething at notthere dot com>
22-Sep-2005 03:58
Hello, actually in answer to your question, I use putenv
regularly to control the timezone info.  Here's the code that
sets both the local environment and SQL to the right
timezone.  I also use it when running a cvs client through the
web.  I will post what format it's looking for next.

<?php
    
//this isn't the most graceful approach, but if it's set, set the tz
    
if (isset($_SESSION[BEE_PROFILE_TZOFFSET])) {
      
putenv('TZ='.$_SESSION[BEE_PROFILE_TZOFFSET]);
      
//format the timezone for SQL
      
$timeZone = preg_replace('/([+-]\d{2})(\d{2})/',
                                            
'\'\1:\2\'', date('O'));
      
mysql_query('SET time_zone='.$timeZone);
     }
?>
otie at ev1 dot net
27-Mar-2005 12:02
trying to change timezones using putenv:
In my case (php 4.2.2) (redhat 2.4.21-1.1931.2.274) TZ does not show up in either the PHP or Linux environment. I'm not sure what the format for TZ is. I tried TZ=EST5EDT. It seemed to work. When I started a session with another browser, it worked there too. Even though I didn't change the environment there.... Hmmm.
When I ran the test without the putenv... the date() function would sometimes show CST (my zone), sometimes EST. Every time I reloaded the page, the time would change zones. All times from a database - not time() time.
Restoring my TZ=CST didn't help. The times would still switch back and forth.
I finally restarted Apache. That worked. The times are now stable.
I think the only solution is subtracting (or adding) the appropriate time difference from the time stamp.

I wonder what the time functions are looking at.

O.
Iavor
08-Feb-2005 06: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 02: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 07: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:

putenv ('TZ=Europe/Amsterdam');
mktime(0,0,0,1,1,1970)
echo date("H:i:s");
johnhill at webtendon dot com
11-Feb-2001 05:46
Here is an example of using putenv to change the timezone for the current script.  You would have to add a putenv() call to each page in which you wanted to change the timezone.  This was posted here about a year ago...I forgot the author's name.  He mentioned to "see the zoneinfo directory-tree on your System for a list". This is usually /usr/share/zoneinfo or /usr/lib/zoneinfo.  It works in PHP3, not sure about PHP4.

  //Set the Timezone
print("server timezone is: " . getenv('TZ') . "<br>\n");
  print("server time is: " . date("H:i:s") . "<br><br>\n");
  print("changing server time zone to US/Pacific....<br><br>\n");
  putenv("TZ=US/Pacific");
  print("new server time is: " . date("H:i:s") . "<br>\n");
  print("new server timezone for this script is: " . getenv('TZ'));

some Unix servers may use a different format for TZ.  Example: putenv("TZ=PST8EDT");
david dot boyce at messagingdirect dot comnospam
14-Sep-2000 01: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:

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