|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-11-26 19:14 UTC] ganlvtech at qq dot com
Description: ------------ If the value ends with odd numbers of Chinese characters, it returns false and getenv cannot get the environment variable just set. If the value ends with even numbers of Chinese characters, it works well and returns true. If the value ends with a ASCII character, although there are some Chinese characters in the string, it still works well. If the Chinese characters is split by ASCII character, only the number of the last part of Chinese characters affects the result. Japanese character also cause this problem. On ubuntu 16.04.3 with php 7.0.22 everything is OK. So the bug may be only for Windows. I have checked the php-src/ext/standard/basic_functions.c PHP_FUNCTION(putenv), but I'm sorry that I cannot find the solution. This problem might be similar to the fixed bug #50690(putenv() does not assign values to env. vars when the value is one character), but this one is for non-ASCII character. Thank you. Test script: --------------- <?php // Chinese character '啊' === urldecode('%E5%95%8A') var_dump(putenv('FOO=啊')); // false // var_dump(putenv('FOO=' . urldecode('%E5%95%8A'))); // false var_dump(putenv('FOO=啊啊')); // true // var_dump(putenv('FOO=' . urldecode('%E5%95%8A%E5%95%8A'))); // true var_dump(putenv('FOO=啊啊啊')); // false var_dump(putenv('FOO=啊啊啊啊')); // true // odd got false, even got true. var_dump(putenv('FOO=啊a')); // true var_dump(putenv('FOO=啊a啊')); // false var_dump(putenv('FOO=啊a啊a')); // true var_dump(putenv('FOO=啊a啊a啊')); // false var_dump(putenv('FOO=啊a啊啊')); // true var_dump(putenv('FOO=啊a啊啊啊')); // false var_dump(putenv('FOO=啊a啊啊啊啊')); // true // If the value ends with odd numbers of Chinese character, it returns false. ?> Expected result: ---------------- bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) Actual result: -------------- bool(false) bool(true) bool(false) bool(true) bool(true) bool(false) bool(true) bool(false) bool(true) bool(false) bool(true) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 09:00:01 2025 UTC |
Oh, if I change the encoding of the test script from UTF-8 to GBK (from 3 bytes per char to 2 bytes per char), it will be OK. I don't know if there is any settings wrong with my Windows (Chinese simplyfied version). It seems that the system only accept non-ASCII char in 2 bytes (in another word, system use GBK encoding). System accept & return the value in GBK encoding, but php pass & retrieve env var in UTF-8 encoding. I set an Environment variable in control panel, and retrieve the value in php by getenv. I got an GBK encoding byte string cannot read. Because php7 use UTF-8 as default ouput encoding. I tried echo iconv('GBK', 'UTF-8', getenv('FOO')); and got the expected value. I think this bug may be the limitation of Windows platform, and may not be fixed unless Windows change the its default encoding to Unicode. Thanks. Another test script: --------------- // Greek_alphabet 'α' === urldecode('%CE%B1') var_dump(putenv('FOO=α')); // true php.ini: --------------- ; PHP's default character set is set to UTF-8. ; http://php.net/default-charset default_charset = "UTF-8" ; PHP internal character encoding is set to empty. ; If empty, default_charset is used. ; http://php.net/internal-encoding ;internal_encoding = PHP Version: php-7.1.12-nts-Win32-VC14-x64Using php.exe got expected result, but using php-cgi.exe putenv did not sets vars with Unicode. Test script: --------------- <?php var_dump(putenv('FOO=啊')); var_dump(`echo %FOO%`); // There will be a "\n" in the result. var_dump(getenv('FOO')); ?> Expected result: ---------------- bool(true) string(4) "啊 " string(3) "啊" Actual result: -------------- bool(true) string(3) "С " string(2) "С" The length of string is not equal to each other. putenv put vars using cp936 instead of Unicode. You can use another test script to view the difference. Test script: -------------- <?php var_dump(putenv('FOO=啊')); echo urlencode(`echo %FOO%`), PHP_EOL; echo urlencode(getenv('FOO')), PHP_EOL; ?> Expected result: ---------------- bool(true) %E5%95%8A%0A %E5%95%8A Actual result: -------------- bool(true) %B0%A1%0A %B0%A1 php snapshot: php-7.1-nts-windows-vc14-x64-r06202f0