safety of setLocale

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

safety of setLocale

Post by jasongr »

Can the PHP setLocale () be used to set the locale on a connection-based level, or it only sets it on a wider level, such as thread-level, process-level, or even server-level?

I would like to know the question for Linux and windows separately.

regards
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

considering how most php scripts are processed, I'd say it's per page request.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

feyd wrote:considering how most php scripts are processed, I'd say it's per page request.
php setlocale is just a wrapper to setlocale defined in Standard C library. Therefore it's platform-dependent. I'd recommend to use the following script to test if certain implementation is thread-safe:

Code: Select all

set_time_limit(0);
$locales = array(
   'C',
   'POSIX',
   'pt_PT.ISO8859-1',
   'ru_RU.KOI8-R',
   'es-ES',
 //... and so forth
 // adjust this array for particular system
);
$localeset = setlocale(LC_ALL, $locales[rand(0, count($locales)-1)]);
sleep(20); // wait for operator to run several instances
if(setlocale(LC_ALL, "0") != $localeset) { 
   echo "<b>Your server isn't thread-safe</b>";
} else {
   echo "<b>Your server is thread-safe</b>";
}
run this script in, say, 10 windows and wait. I wouldn't recommend to use setlocale if any of these windows told you that your server wasn't thread-safe.
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

thanks
I will give this a try
and will post my findings
Post Reply