r/PHPhelp • u/viewofalake • May 23 '23
Does setlocale() affect POST or REQUEST data?
My understanding was that it controlled the generation of locale specific content e.g., date formats, etc.
It was suggested to me that the problem I'm seeing related to utf-8 request data decoding COULD be related to locale, i.e., that locale can affect how utf-8 is decoded into codepoints.
Is this possible?
On a bit of a tangent: On php7.4 (+ Apache 2.4), I'm seeing something odd:
The default 'LANG' in envvars is 'C'. This is reflected in the return of setlocale(LC_CTYPE,0);
When I change it to "en_US.UTF-8", restart apache and php-fpm, a call to setlocale(LC_CTYPE,0) will reflect the change. BUT..., at some point, on successive requests, the setlocale() call will flip back to returning 'C'. It ain't me doing it..., and it doesn't appear to be any of the modules I'm using either.
FWIW..., threads are not enabled.
2
u/kAlvaro May 24 '23
PHP strings are binary data, with no encoding information. Neither $_POST nor $_REQUEST values are decoded at all: what client sends is what PHP gets.
2
u/viewofalake May 24 '23
That's a good point. Don't know if you saw the link to the original bug posting, but it does help narrow things down a bit. Frustrating..., I need to borrow an iPhone and setup for better debugging.
3
u/allen_jb May 23 '23
No. locale settings do not affect how PHP interprets request data.
The problem with using setlocale is that it's a process specific setting - when something changes locale, it changes it for the whole process, which may not be just the current script. See the red box on the manual page: https://www.php.net/manual/en/function.setlocale.php
In the default setup, php-fpm will run multiple scripts under the same process. If any script changes the locale, that setting will apply to all future scripts until that worker dies.
Generally I recommend avoiding setlocale and using the intl extension utilities instead (where possible). In my experience, in modern PHP, there should be very little reason to use setlocale.
It would be useful to have more information on the specific (original) problem you're trying to solve.