Bug->Doc #79875 [Opn->Ver]: getenv() can't get the right binary string passed by putenv()
| From: | cmb@php.net | Date: | Sat, 18 Jul 2020 20:14:04 +0000 |
| Subject: | Bug->Doc #79875 [Opn->Ver]: getenv() can't get the right binary string passed by putenv() | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-17710@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79875&edit=1
ID: 79875
Updated by: cmb@php.net
Reported by: paulbalandan at gmail dot com
Summary: getenv() can't get the right binary string passed by
putenv()
-Status: Open
+Status: Verified
-Type: Bug
+Type: Documentation Problem
Package: *General Issues
Operating System: Windows 10
PHP Version: 7.4.8
Block user comment: N
Private report: N
New Comment:
As of PHP 7.1.0, PHP on Windows has transparent support for
Unicode file paths[1], and that also applies to environment
variables (the values and the keys). Since environment variables
are usually character strings (and not general binary strings),
this increases interoperability overall. I would suggest not to
store arbitrary binary strings as values of environment variables,
but as character strings instead (e.g. in this case you could
store the hexadecimal string in the first place, and hex2bin() it
after retrieval). If you absolutely need to store such binary
strings in environment variables, you should convert them first to
Unicode (i.e. UTF-8, because UTF-16 and UTF-32 code pages are only
available to managed applications[2], and UTF-7 is ⦠to be
avoided), e.g.
<?php
$bin = sapi_windows_cp_conv(
sapi_windows_cp_get('ansi'),
65001, // UTF-8 codepage
hex2bin('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6')
);
putenv("key=$bin");
$bin2 = getenv('key', true);
var_dump(bin2hex($bin) === bin2hex($bin2));
?>
So this is expected, but currently undocumented behavior.
[1] <https://www.php.net/manual/en/migration71.windows-support.php#migration71.windows-support.long-and-utf8-path>
[2] <https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers>
Previous Comments:
------------------------------------------------------------------------
[2020-07-18 17:02:03] paulbalandan at gmail dot com
Description:
------------
In Windows when posting binary string to the environment via putenv and then getting it back via
getenv, the results are different.
Test script:
---------------
<?php
$bin = hex2bin('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6');
putenv("key=$bin");
$bin2 = getenv('key', true);
var_dump(bin2hex($bin) === bin2hex($bin2));
Expected result:
----------------
bool(true)
Actual result:
--------------
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79875&edit=1