9

How can I check the language of user's browser by PHP?

I need to show a different page for people in US and in UK.

I tried the following code unsuccessfully

<?php
if(ereg("us", $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
   include('http://page.com/us.txt');
else
   include('http://page.com/uk.txt');
?>

I run a specific code for people in US and for them in UK.

2
  • Do you mean "people in US" or "people with language: en-us"? What happens when I take my en-uk notebook to the U.S. of A.? Commented Mar 6, 2009 at 21:19
  • I mean only people who have the language setting as en-us. Commented Mar 6, 2009 at 21:25

4 Answers 4

8

Likely just a case sensitivity issue; eregi('en-us') or preg_match('/en-us/i') should have picked it up.

However, just looking for ‘en-us’ in the header may get it wrong sometimes, in particular when both the US and UK languages are listed. “Accept-Language” is actually quite a complicated header, which really you'd want a proper parser for.

If you have PECL the whole job is already done for you: http://www.php.net/manual/en/function.http-negotiate-language.php

I don't know why the other answers are going for the User-Agent header; this is utterly bogus. User-Agent is not mandated to hold a language value in any particular place, and for some browsers (eg. Opera, and some minor browser I've never heard of called ‘Internet Explorer’) it will not at all. Where it does contain a language, that'll be the of language the browser build was installed in, not the user's preferred language which is what you should be looking at. (This setting will default to the build language, but can be customised by the user from the preferences UI.)

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

1 Comment

Do you mean to replace HTTP_USER_AGENT by HTTP_ACCEPT_LANGUAGE in the above code?
3

Try this:

<?
if(preg_match('/en-us/i', $_SERVER['HTTP_USER_AGENT']))
    include('http://page.com/us.txt');
else
    include('http://page.com/uk.txt');
?>

3 Comments

HTTP_USER_AGENT will match most of those who have the en-us version of the browser. You should be checking HTTP_ACCEPT_LANGUAGE
Piskvor: I don't know, the general Firefox download shows as en-GB for me.
case insensitve: preg_match('/en-us/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'])
2

A probably more reliable way of doing this is to perform a regex on the $_SERVER['HTTP_USER_AGENT'] string.

<?php
  if(preg_match('/en-US/', $_SERVER['HTTP_USER_AGENT']))
    include('http://page.com/us.txt');
  else
    include('http://page.com/uk.txt');
?>

You are not guaranteed to get a valid and useful user-agent string, so make sure that the else statement contains a reasonable alternative.

1 Comment

ah, shoot. I saw that you accepted then de-accepted the answer. Looks like I forgot the forward slashes (as chaos found).
1

This is a zend based solution. It will also work when you add other languages.

<?php

include_once "Zend/Locale.php";

$zend_locale = new Zend_Locale(Zend_Locale::BROWSER);

// returns en for English, de for German etc.
echo $browser_language = $zend_locale->getLanguage();
echo "<br />\n";
// returns en_US for American English, en_GB for British English etc.
echo $browser_locale = $zend_locale->toString();
echo "<br />\n";

Solution seen on:

http://www.mpopp.net/2010/07/how-to-detect-the-users-preferred-language-smarter-than-google/

Comments

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.