note 8496 modified in function.putenv by danbrown

From: Date: Sun, 20 Jul 2008 01:03:55 +0000
Subject: note 8496 modified in function.putenv by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141884@lists.php.net to get a copy of this message
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. --was-- 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. http://php.net/manual/en/function.putenv.php

« previous php.notes (#141884) next »