redirect based on default browser locale

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
ablueflame
Forum Newbie
Posts: 1
Joined: Mon Apr 28, 2008 12:05 pm

redirect based on default browser locale

Post by ablueflame »

Hi guys,
I have a site which supports three different languages, English, Polish & Italian. I am detecting the user's language and then redirecting them to the site which matches their language. So for example if you go to the site using a Polish browser you are redirect to pl.blog.website.

Everything works fine except in IE6/IE7 :(

The problem is that if you have a secondary locale in your browser such as Polish or Italian then IE6/7 brings you to the Polish site before the English sit, even if English is the default language on your browser. This is because pl appears in the if/else statement before English.

I need to ammend the below code so that only the default language gets returned as the $lang variable. Any ideas on how I can do this?

Code: Select all

<?php  
$lang = ($_SERVER['HTTP_ACCEPT_LANGUAGE']);  
 
if(ereg("pl", $lang)) {  
    header("location: http://pl.blog.website.com");  
} 
else if(ereg("it", $lang)) {  
    header("location: http://it.blog.website.com");  
}else {  
    header("location: http://en.blog.website.com");  
}  
?>
Post Reply