views:

678

answers:

5

Hi,

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.

+2  A: 

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/");
}
Ivan Nevostruev
I try this code but when I run website given me loop error "The page isn't redirecting properly"
jack55
Try to use `header("Location: new_page.html");` instead of `http://it2.in/`
Ivan Nevostruev
also not working :(
jack55
Try modified version of code above and add `error_reporting(E_ALL)` to the begining of your script.
Ivan Nevostruev
After the line `header("Location: http://it2.in/");`, write `die;`
chelmertz
+1  A: 

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/");}

?>
svens
+1  A: 
$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

elcuco
+1 for suggesting GetText
Luc M
in language.php variable like that:$title and in HTML :<?=$title?>
jack55
elcuco
+1  A: 

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 :)

tawfekov
A: 
<?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/');
?>
w35l3y
given me loop error "The page isn't redirecting properly"
jack55