redirect based on default browser locale

Discussion in 'PHP' started by 2bnumber1, Apr 28, 2008.

  1. #1
    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:

     
    2bnumber1, Apr 28, 2008 IP
  2. WebGodzilla

    WebGodzilla Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    WebGodzilla, Apr 28, 2008 IP
  3. 2bnumber1

    2bnumber1 Peon

    Messages:
    211
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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]
     
    2bnumber1, Apr 29, 2008 IP
  4. WebGodzilla

    WebGodzilla Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    WebGodzilla, May 4, 2008 IP