Jump to content

Language selection


Beppis

Recommended Posts

Hi! :) I want to have a first page on my website where you as visitor can choose the preferred language. No login at all.

I want to save this choice in a cookie or something. The easiest way :)

So next time he/she visits, the language choice is already made for he/she.

 

Any good suggestions for a php code that works? :)

Link to comment
Share on other sites

You could have it so on the index page it checks if the cookie exists and if not it goes to language.php

 

Something like this:

 

index.php

<?php
if(!isset($_COOKIE['language']))
{
    header("Location: language.php");
}
else
{
$language = $_COOKIE['language'];
?>

<html>
<head>
<title>Main Page</title>
</head>
<body>
<h1>Main Page</h1>
<?php echo 'Selected language: '.$language; ?>
</body>
</html>
<?php 
}
?>

 

language.php

<?php

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
    
<html>
<head>
<title>Site Name - Language Selection</title>
</head>
<body>
<h1>Language Selection</h1>
<form action="language.php" method="post">
<select name="language" id="language">
<option value="english" selected="selected">English</option>
<option value="german">German</option>
<option value="french">French</option>
<option value="spanish">Spanish</option>
<option value="andmore">And more...</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>


    <?php
    }
    else
    {
        $language = $_POST['language'];
        setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds
        header("Location: index.php");
    }
}
else
{
    header("Location: index.php");
}

?>

 

You would need to change this code to make the index page do something with the language to display that language.

Link to comment
Share on other sites

Does your website already have all the language files setup?

 

And different code to make it display a different page per language:

 

language.php

<?php

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
    
<html>
<head>
<title>Site Name - Language Selection</title>
</head>
<body>
<h1>Language Selection</h1>
<form action="language.php" method="post">
<select name="language" id="language">
<option value="english" selected="selected">English</option>
<option value="german">German</option>
<option value="french">French</option>
<option value="spanish">Spanish</option>
<option value="andmore">And more...</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>


    <?php
    }
    else
    {
        $language = $_POST['language'];
        setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds
        
        switch($language)
        {
            case english:
                $redirect = "index.php";
                break;
            case german:
                $redirect = "german/index.php";
                break;
            case french:
                $redirect = "french/index.php";
                break;
            case spanish:
                $redirect = "spanish/index.php";
                break;
            default:
                $redirect = "index.php";
        }
        
        header("Location: $redirect");
    }
}
else
{
    $language = $_COOKIE['language'];
    
    switch($language)
    {
        case english:
            $redirect = "index.php";
            break;
        case german:
            $redirect = "german/index.php";
            break;
        case french:
            $redirect = "french/index.php";
            break;
        case spanish:
            $redirect = "spanish/index.php";
            break;
        default:
            $redirect = "index.php";
        }
        
    header("Location: $redirect");
}

?>

Link to comment
Share on other sites

You could have more than 1 submit/image button with different names and have each one have an image of a flag on it, something like this:

 

<form action="language.php" method="post">
<input type="image" src="images/flags/english.png" width="16px" height="11px" border="0" name="submitenglish" alt="English">
<input type="image" src="images/flags/german.png" width="16px" height="11px" border="0" name="submitgerman" alt="German">
<input type="image" src="images/flags/swedish.png" width="16px" height="11px" border="0" name="submitswedish" alt="Swedish">
</form>

Link to comment
Share on other sites

Parse error: syntax error, unexpected $end in Line 106

 

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? include("title.php"); ?></title>
<style type="text/css">
<!--
body {
background-image: url(images/background.jpg);
}
-->
</style></head>

<body>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><br />
  <br />
</p>
<div align="center">
<h1>Language Selection</h1>
  <table width="200" border="0">
    <tr>
    <form action="language.php" method="post">
    <td width="114"><div align="left"><input type="image" src="images/swe.jpg" border="0" name="submitswedish" alt="Swedish"></div></td>
         <td width="1"> </td>
      <td><div align="left"></div></td>
    <td><input type="image" src="images/usa.jpg" border="0" name="submitusa" alt="Usa"></td>
</form>
    </tr>
    <tr>
      <td><div align="center"><strong>SWE</strong></div></td>
      <td> </td>
      <td width="0"><div align="center"></div></td>
      <td width="110"><div align="center"><strong>USA</strong></div></td>
    </tr>
  </table>
  
</div>
<p align="center"> </p>
</body>
</html>
<?php

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
        <?php
    }
    else
    {
        $language = $_POST['language'];
        setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds
        
        switch($language)
        {
            case usa:
                $redirect = "index.php";
                break;
            case swedish:
                $redirect = "sv/index.php";
                break;
            default:
                $redirect = "index.php";
        }
        
        header("Location: $redirect");
    }
}
else
{
    $language = $_COOKIE['language'];
    
    switch($language)
    {
        case usa:
            $redirect = "index.php";
            break;
        case swedish:
            $redirect = "sv/index.php";
            break;
        default:
            $redirect = "index.php";
        }
        
    header("Location: $redirect");
}

                                                                                    LINE 106!!

Link to comment
Share on other sites

Try:

 

<?php

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? include("title.php"); ?></title>
<style type="text/css">
<!--
body {

    

background-image: url(images/background.jpg);
}
-->
</style></head>

<body>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><br />
  <br />
</p>
<div align="center">
<h1>Language Selection</h1>
  <table width="200" border="0">
    <tr>
    <form action="language.php" method="post">
    <td width="114"><div align="left"><input type="image" src="images/swe.jpg" border="0" name="submitswedish" alt="Swedish"></div></td>
         <td width="1"> </td>
      <td><div align="left"></div></td>
    <td><input type="image" src="images/usa.jpg" border="0" name="submitusa" alt="Usa"></td>
</form>
    </tr>
    <tr>
      <td><div align="center"><strong>SWE</strong></div></td>
      <td> </td>
      <td width="0"><div align="center"></div></td>
      <td width="110"><div align="center"><strong>USA</strong></div></td>
    </tr>
  </table>
  
</div>
<p align="center"> </p>
</body>
</html>
<?php

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
        <?php
    }
    else
    {
        $language = $_POST['language'];
        setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds
        
        switch($language)
        {
            case usa:
                $redirect = "index.php";
                break;
            case swedish:
                $redirect = "sv/index.php";
                break;
            default:
                $redirect = "index.php";
        }
        
        header("Location: $redirect");
    }
}
else
{
    $language = $_COOKIE['language'];
    
    switch($language)
    {
        case usa:
            $redirect = "index.php";
            break;
        case swedish:
            $redirect = "sv/index.php";
            break;
        default:
            $redirect = "index.php";
        }
        
    header("Location: $redirect");
}
}
}

?>

Link to comment
Share on other sites

Why not just pass the language in the URL? e.g. "example.com/index.php?lang=en"

 

Then in the code you just need to check if the lang param has been passed, else use the cookie, else use the default:

 

if (isset($_GET['lang']))
{
    // return lang (convert it to entities
    // to prevent any XSS attacks)
    $lang = htmlspecialchars($_GET['lang']);

    // store for next time
    setcookie('lang', htmlspecialchars($_GET['lang']));
}
elseif (isset($_COOKIE['lang']))
{
    // set lang var
    $lang = $_COOKIE['lang'];
}

if (!isset($lang))
{
    // if no $lang var set, use default
    $lang = 'en';
}

Link to comment
Share on other sites

Thanks again :)

 

But nothing happens when I click on the images :o It's not directing me to a page...

 

Here's the code:

<?

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? include("title.php"); ?></title>
<style type="text/css">
<!--
body {
background-image: url(images/background.jpg);
}
-->
</style></head>

<body>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><br>
    <br>
  <br />
  <br />
</p>
<div align="center">
  <table width="200" border="0">
    <tr>
    <form action="language.php" method="post">
    <td width="114"><div align="left"><input type="image" src="images/swe.jpg" border="0" name="swedish" alt="swedish"></div></td>
         <td width="1"> </td>
      <td><div align="left"></div></td>
    <td><input type="image" src="images/usa.jpg" border="0" name="usa" alt="ssa"></td>
</form>
    </tr>
    <tr>
      <td><div align="center"><strong>SWE</strong></div></td>
      <td> </td>
      <td width="0"><div align="center"></div></td>
      <td width="110"><div align="center"><strong>USA</strong></div></td>
    </tr>
  </table>
  
</div>
<p align="center"> </p>
</body>
</html>
<?php

if (isset($_GET['language']))
{
    // return lang (convert it to entities
    // to prevent any XSS attacks)
    $lang = htmlspecialchars($_GET['language']);

    // store for next time
    setcookie('language', htmlspecialchars($_GET['language']));
}
elseif (isset($_COOKIE['language']))
{
    // set lang var
    $lang = $_COOKIE['language'];
}

if (!isset($lang))
{
    // if no $lang var set, use default
    $lang = 'en';
}

if(!isset($_COOKIE['language']))
{
    if(!isset($_POST['submit']))
    {
    ?>
        <?php
    }
    else
    {
        $language = $_POST['language'];
        setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds
        
        switch($language)
        {
            case usa:
                $redirect = "http://www.clickbuxer.com";
                break;
            case swedish:
                $redirect = "http://www.clickbuxer.com/sv/index.php";
                break;
            default:
                $redirect = "http://www.clickbuxer.com";
        }
        
        header("Location: $redirect");
    }
}
else
{
    $language = $_COOKIE['language'];
    
    switch($language)
    {
        case usa:
            $redirect = "http://www.clickbuxer.com";
            break;
        case swedish:
            $redirect = "http://www.clickbuxer.com/sv/index.php";
            break;
        default:
            $redirect = "http://www.clickbuxer.com";
        }
        
    header("Location: $redirect");
}
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.