Intro Hi, we are building a multi-language website for a little hotel as a project to deepen our web development skills. It's a cool project so far and together with Stackoverflow, we could solve a lot of doubts. Dope! However, I've been researching a lot but none of the multi-language php posts (although somewhat helpful) seem to answer what we're looking for (or we can't translate the problem's solution to our case). We have content for different sections of in total 5 pages (Home; About;The Rooms; Gallery; Contact) stored in a php MyAdmin database in 4 different languages and defined $_Sessions for each language retrieval. The titles and smaller amounts of text, e.g. a button saying "We guarantee you the lowest rate" are stored in arrays for each language. When initiating the page, all the content is displayed in English, so that we know that both the arrays work and the database is connected.
The Problem Whenever I try to select the language choosing the dropdown language menu on the website, the page (and any other page) just reloads with english. How do I make the Sessions change? Do I need cookies? Do I need to use "Get?" If yes, how?
Here's part of my index.PHP.
Each list object has a lang_id that we defined in numbers (1,2,3,4,5, and so on) as opposed normally to en, ge, fr, pt, es etc.
I want each list item to change according to its session, e.g. English to 1, German to 2... so then it can at the same time retrieve the data from the arrays + the values stored within the database.
<div class="language">
<span><?php echo _t_language; ?></span> //_t_language comes from the array lang_en/gr...
<ul>
<li>
<a href="languages/switch_lang.php?lang=1">
<?php echo _t_english; ?>
</a>
</li>
<li>
<a href="languages/switch_lang.php?lang=2">
<?php echo _t_german; ?>
</a>
</li>
<li>
<a href="languages/switch_lang.php?lang=3">
<?php echo _t_french; ?>
</a>
</li>
(...)
</ul>
</div>
I made a languages folder that includes switch_lang.php and define_lang.php. The session is started in define_lang.php, that is included before starting the HTML. Here's the code of both of them:
switch_lang.php:
<?php
//get current url
$goback=$_SERVER['HTTP_REFERER'];
$lang=$_GET['lang'];
$_SESSION['lang']=$lang;
header("location: $goback");
?>
define_lang.php:
<?php
session_start();
//give session when first load the page
if($_SESSION["lang"]==""){
$_SESSION["lang"]="1";
}
if($_SESSION['lang']=='1') {
include('lang_en.php');
}else if($_SESSION['lang']=='2') {
include('lang_gr.php');
}else if($_SESSION['lang']=='3') {
include('lang_fr.php');
}else if($_SESSION['lang']=='4') {
include('lang_it.php');
}else if($_SESSION['lang']=='5') {
include('lang_pt.php');
}else if($_SESSION['lang']=='6') {
include('lang_es.php');
}
?>
Do we have to redirect anything? Do we have to use cookies? Do we have to use $_Get? Does it work without $_Get, too? How does it all fit together??
It's almost 12 'o clock and, as many other people here, we are beyond realizing what's wrong. Any help would be so much appreciated and at any time later, we are ready to give back advice. Thanks and have a nice night, Spastnudel & Nina
</div>