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? <?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"); } ?> PHP:
I guess you shouldn't use ereg here. After all, language codes are a standard, and the string won't have different variants... I guess a case switch will work better.
Ah!, Cheers. I'm not the best at PHP. I am using this code now but it's not working for me. When I test it out using either a Polish or an Italian browser, I'm being brought to the English blog. Any ideas on where the switch is going wrong? Ok, So I think the problem is occuring because I am using ereg to determine the browser language when I should be using cases. I am using this code now but it's not working for me. Any ideas where I might be going wrong? [PHP]<?php $lang = ($_SERVER['HTTP_ACCEPT_LANGUAGE']); switch ($lang) { case "pl": header("location: http://pl.blog.website.com"); case "it": header("location: http://it.blog.website.com"); default: header("location: http://en.blog.website.com"); } ?> PHP: [/PHP]
The official PHP Manual says that if you write your switch like that, you will always have the last option — just like in your case. That's foolish, but that's the way they made it. You should break the switch after every option.