country and language detection

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
pppswing
Forum Commoner
Posts: 33
Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia

country and language detection

Post by pppswing »

Hello,
I would like to know if there is already some php script that detect country and language. Language of the browser should be easy to find but country depends on many more facts.

Thanks :D
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

To get the languages a browser accepts:

Code: Select all

function getLanguages()
{
  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  {
    $parts = preg_split("/;/",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
    $langs = preg_split("/,/",$parts[0]);
    return $langs;
  }
  else
  {
     return null;
   }
}
To get the country (i know you could also have a look at an eventual http_referer etc) i use geoip:

Code: Select all

function getCountry()
{
  require_once('geoip.inc');
  $address = $_SERVER['REMOTE_ADDR']; 
  $gi = geoip_open('GeoIP.dat',GEOIP_STANDARD);
  $countrycode = geoip_country_code_by_addr($gi,$address);
  geoip_close($gi);
  return $countrycode;
}
Post Reply