jack55
jack55

Reputation: 1

PHP multilanguage site

I want make multilanguage site with PHP based on SESSIONS like this site untiny.com

I try with this code but not working :

    <?

session_start();
$lang = $_GET['lang'];
if (!isset($lang)) {
    include ('ar/language.php');
    $lang = "ar";
}
else if ($lang == "en" ) {include ('en/language.php'); $SESSION["lang"] = "en"; header("Location: http://it2.in/");}
else if ($lang == "ar" ) {include ('ar/language.php'); $SESSION["lang"] = "ar"; header("Location: http://it2.in/");}
else if ($lang != "ar" || "en") {header("Location: http://it2.in/"); header("Location: http://it2.in/");}

?>

Anyone can help me. Thanks


Thank you all. But @ now nothing working, Are there other ideas.

Upvotes: 0

Views: 7577

Answers (5)

Ivan Nevostruev
Ivan Nevostruev

Reputation: 28713

Probably your problem is in last line. It should work like:

else if ($lang != "ar" || $lang != "en") {header("Location: http://it2.in/");}

Also I suggest you to create a separate array to store available languages

$known_languages = array('en', 'ar'); ## just add new language here when you need
session_start();

## if language is stored in SESSION then use it, otherwise use GET params
if (array_key_exists('lang', $_SESSION)) {
    $lang = $_SESSION['lang'];
    include($lang.'/language.php'); 
    ## echo "You current language is <strong>$lang</strong>";
    include("page.php");
}
else {
    $lang = $_GET['lang'];

    ## if language is not set or is not available, then use default value
    if (!isset($lang) || !in_array($lang, $known_languages) {
        $lang = "ar";
    }
    include($lang.'/language.php'); 
    $SESSION["lang"] = $lang; 
    header("Location: http://it2.in/");
}

Upvotes: 2

w35l3y
w35l3y

Reputation: 8763

<?php
session_start();

$lang = &$_SESSION['lang'];
$lang = $_GET['lang'];
switch ($lang)
{
    case 'en' :
        include ($lang . '/language.php');
        break;
    case 'ar' : 
    default :
        include ('ar/language.php');
        $lang = 'ar';
}

header('Location: http://it2.in/');
?>

Upvotes: 0

tawfekov
tawfekov

Reputation: 5122

When you are using header function always consider using exit(); after it to stop the execution of the code

<?php
session_start();
$lang = $_GET['lang'];
if (!isset($lang)) {
    include ('ar/language.php');
    $lang = "ar";
}
else if ($lang == "en" ) {
include ('en/language.php');
$SESSION["lang"] = "en"; 
header("Location: http://it2.in/");
exit(); 
}
else if ($lang == "ar" ) {
include ('ar/language.php');
 $SESSION["lang"] = "ar"; 
header("Location: http://it2.in/");
exit();
}
else if ($lang != "ar" || $lang != "en") {
header("Location: http://it2.in/"); 
exit(); 
}

?>

now you should be redirected to your wanted page :)

Upvotes: 1

elcuco
elcuco

Reputation: 9208

$lang= $_GET['lang'];
include $lang . "/language.php";

Php by default disables those kind of includes, so you will have to enable it manually.

The real question is: what is in language.php?

// en/language.php
$MESSAGES[0] = "Hello";
// es/language.php
$MESSAGES[0] = "Hola";
// fr/language.php

Then in your code you do:

print "<h1>" . $MESSAGES[0] . "</h1>";

This will not scale up, and your head will explote very fast (wait, the message is 1023? or 1022? or 2149?). Please consider porting your code to GetText, which IMHO is a better solution and lets you add new languages without new code. Here is the first hit from google which will give you a head start. If you need more info, please look arround. http://www.phpdig.net/ref/rn26.html

Upvotes: 1

svens
svens

Reputation: 11628

Could you explain what exactly doesn't work?

There's an error in your if-statement. The last else-if is always true, because you're ORing the result from the comparison with the string "en". An else statement will do the job.

<?

    session_start();
    $lang = $_GET['lang'];
    if (!isset($lang)) {
        include ('ar/language.php');
        $lang = "ar";
    }
    else if ($lang == "en" ) {include ('en/language.php'); $SESSION["lang"] = "en"; header("Location: http://it2.in/");}
    else if ($lang == "ar" ) {include ('ar/language.php'); $SESSION["lang"] = "ar"; header("Location: http://it2.in/");}
    else {header("Location: http://it2.in/"); header("Location: http://it2.in/");}

?>

Upvotes: 1

Related Questions