php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74143 putenv can't update existing environment variables, still returns true
Submitted: 2017-02-21 23:08 UTC Modified: 2017-02-24 23:15 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: greg dot bowler at g105b dot com Assigned: googleguy (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Linux Ubuntu 16.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: greg dot bowler at g105b dot com
New email:
PHP Version: OS:

 

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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-02-24 21:41 UTC] googleguy@php.net
-Package: FPM related +Package: Documentation problem
 [2017-02-24 21:41 UTC] googleguy@php.net
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
 [2017-02-24 23:15 UTC] googleguy@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: googleguy
 [2017-02-24 23:15 UTC] googleguy@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

This has been fixed in the latest revision of the documentation.

http://svn.php.net/viewvc?view=revision&revision=341970
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 27 06:01:27 2024 UTC