0

I'm trying to find out system locale from PHP script on Debian. The way I do:

    <?php
    $out = shell_exec('locale');
    print_r($out);
    ?>

Result:

    LANG=C
    LANGUAGE=
    LC_CTYPE="C"
    LC_NUMERIC="C"
    ...

But when I write 'locale' command in my system console I get 'right' result:

    LANG=ru_RU.UTF-8
    LANGUAGE=
    LC_CTYPE="ru_RU.UTF-8"
    LC_NUMERIC="ru_RU.UTF-8"
    ...

What is the right way to get system locale from PHP script?

Overall purpose: In the request I accept a file path, which can contain cyrillic symbols. Then I need to access files in this directory. So I want to make my php script work with systems which use different locales (e.g. ru_Utf8, Koi8-r).

6
  • 3
    Are you sure that the system locale is truly the system's locale, and not just your user's locale? Commented Oct 13, 2015 at 16:04
  • 1
    Apache with php module is run under 'www-data' user. I supposed that php would return its locale, but I'm not sure. From command line 'locale' command returns utf8 under www-data. Commented Oct 13, 2015 at 16:23
  • 1
    What do you have in /etc/default/locale? Commented Oct 13, 2015 at 16:30
  • @zneak it's empty. Please, read my edit of question, it may state the problem more clearly, thx :) Commented Oct 13, 2015 at 17:41
  • 1
    Any reason not to have locale be set based on a data point in the user table? Commented Oct 13, 2015 at 17:47

1 Answer 1

1

One method to get the true system locale from PHP:

1) create a .sh script containing the locale command (and of course the #!/bin/bash header)

2) Add to /etc/sudoers www-data ALL=NOPASSWD: /path/to/locale.sh with the sh script you just created

3) Update your PHP shell_exec to $out = shell_exec('sudo /bin/bash /path/to/locale.sh')

This would allow locale to execute as root, without requiring a password.

Sign up to request clarification or add additional context in comments.

3 Comments

I wouldn't advise modifying the sudoers file just for the sake of reading environment variables. Besides, how can you be sure that root wil have ru_RU.UTF-8?
You can't be sure. But when someone asks for "system" state, my mind defaults to the root user in linux as being the "system". Also agree it's silly to mess with sudoers for something like this.
@zneak thx for answer, I think that I won't be granted rights to do this staff on server. Please, check my edit on question it states the problem more clearly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.