|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-02-21 23:08 UTC] greg dot bowler at g105b dot com
Description:
------------
The putenv function sets the value of an environment variable. According to the manual, putenv returns true on success, false on failure.
The scope of this bug report tracks setting an existing environment variable to a new value. The putenv function sometimes returns true without updating the environment variable for the current session.
To reproduce this issue, set an environment variable in a webserver using PHP FPM, by using the fastcgi_param directive. See test script below for example.
Test script:
---------------
Webserver config file:
----------------------
fastcgi_param TESTVAR_ENV old-value;
test.php:
---------
var_dump(getenv("TESTVAR_ENV"));
var_dump(putenv("TESTVAR_ENV=new-value"));
var_dump(getenv("TESTVAR_ENV"));
Output:
-------
string(12) "old-value"
bool(true)
string(12) "old-value"
Expected result:
----------------
One of the two outcomes are expected:
1. putenv returns false, as there is a failure in setting the variable.
2. getenv returns the newly updated value.
Actual result:
--------------
The reason I consider this a bug is due to the manual stating that putenv will return true on success.
1. putenv is returning true, indicating the variable has been successfully set.
2. getenv is returning the old value, not updated.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 10:00:01 2025 UTC |
I'm changing this into a documentation bug since the behavior you describe is actually just undocumented rather than an actual bug in PHP. When you using getenv() there is an undocumented second boolean parameter, which when set to true, will only return the local value. Since PHP's environment and the environment your nginx web server runs in are isolated by design, getenv() will by default return value set by fastcgi_param here unless you explicitly ask for the local_only copy. So fastcgi_param TESTVAR_ENV old-value; var_dump(getenv("TESTVAR_ENV")); var_dump(putenv("TESTVAR_ENV=new-value")); var_dump(getenv("TESTVAR_ENV", true)); Will return new-value as expected. See source for reference: https://github.com/php/php-src/blob/php-7.1.2/ext/standard/basic_functions.c#L4040