Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Redirecting users to a different homepage depending on their language??

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    I'm going to do this using php.

    The following first piece of code will check if there is a cookie set with the name user_language.

    If it is set it will get it's value (which as you see from using the sha1 function I have encrypted its value when we created it) and compares it to the values it could have been set to.

    Based on each case the user is redirected to wherever you want.

    This should be at the top of your home page (the landing page where the users chooses the language).

    If the cookie is not set the code after the php code in the index.php page will be displaed.

    /*********************index.php (which is basically www.yoursite.com which is also basically www.yoursite.com/index.php ******************/

    <?php

    $cookie_name = 'user_language';

    //check if cookie is set

    if(isset($_COOKIE[$cookie_name])){

    //get the value of the cookie

    $cookie_value = $_COOKIE[$cookie_name];

    //check the value of the cookie

    swicth($cookie_value){

    case sha1("en"): header('location:/www.yoursite.com/en/'); break;

    case sha1("fr"): header('location:/www.yoursite.com/fr/'); break;

    case sha1("sp"): header('location:/www.yoursite.com/sp/'); break;

    deafult: break;

    }#end of switch

    }#end of conditional if

    ?>

    <!DOCTYPE html>

    <html>

    <head>

    <title>Welcome to yoursite</title>

    <meta charset="utf-8">

    </head>

    <body>

    Choose a language:

    <form method="post" action="setLanguage.php">

    <input type="radio" name="language" value="en">English<br>

    <input type="radio" name="language" value="fr">French<br>

    <input type="radio" name="language" value="sp">Spanish<br>

    <input type="submit" value="submit">

    </form>

    </body>

    </html>

    Now we want to create the setLanguage.php page where we set a cookie and then redirect the user based on the language they set. Later on as long as the cookie is set the user will automatically be redirected.

    First we check if the submit button is clicked but the user did not choose any language.

    If so, redirect back to index.php where they will have to choose a language.

    If the submit button is clicked and the user picked a language we will sanitize the post variable, never trust any user data.

    Then we will set a cookie using the set_a_cookie function. Inside this function we will just set a cookie with the name in the parameters of the function and the value of the other parameter in the function. It will be set to 30 days since 84600 = 1 day, and times 30 then it's 30 days.

    After that we will redirect the user to the respective directory suppose $language from POST is fr then the directory will be www.yoursite.com/fr

    Of course you can change the location of the redirect.

    We also encrypt the value of the cookie before passing it to the set_a_cookie function we created.

    /**************setLanguage.php (which is www.yoursite.com/setLanguage.php) ***************/

    <?php

    //redirect user back to home page if the user did not choose any language

    if(isset($_POST['submit']) && (!isset($_POST['language']) || empty($_POST['language']))){

    header('location:/www.yoursite.com/');

    }

    //a function to set a cookie, could be modified based on your needs

    function set_a_cookie($cookie_value, $cookie_name){

    setCookie($cookie_name, $cookie_value, (86400 * 30));

    }

    //data is set, proceed to set a cookie and redirect

    if(isset($_POST['submit'] && isset($_POST['language'] && !empty($_POST['language']))){

    //sanitize user input

    $language = htmlspecialchars($_POST['language']);

    //set a cookie

    set_a_cookie(sha1($language), 'user_language');

    //redirect

    header('location:/www.yoursite.com/'.$language);

    }

    ?>

    This can easily be done in one file (index.php) but I wanted to separate things to make it easier.

    Hope I helped, good luck!

  • 7 years ago

    I want to know if there is anyway to redirect users depending on their language

    for example:

    the first time they enter the site, it gives them a list of 5 languages

    if they pick English it will redirect them to the English home page,

    if they pick French it would redirect them to the French home page

    if they pick Spanish it will redirect them to the Spanish home page

    and so on...

    but I want to have a cookie to save their choice and automatically redirect them to the link of their choice

    can anyone help me with this cause I'm stuck

    thank you

Still have questions? Get your answers by asking now.