Forum Moderators: coopster
function setLang($session_lang,$post_lang){
$_SESSION['lang'] = $session_lang;
$_POST['L'] = $post_lang;
if ($_SESSION['lang']) {
if ($_POST['L']){
if ($_POST['L'] == $_SESSION['lang']) {
unset($_POST['L']);
return $_SESSION['lang'];
}
else {
session_unregister ('lang');
$_SESSION['lang'] = '';
session_register('lang');
$_SESSION['lang'] = $_POST['L'];
return $_SESSION['lang'];
}
}
else {
return $_SESSION['lang'];
}
}
else {
if ($_POST['L']) {
session_register('lang');
$_SESSION['lang'] = $_POST['L'];
return $_SESSION['lang'];
}
else {
$_SESSION['lang'] = "GB";
return $_SESSION['lang'];
}
}
return $_SESSION['lang'];
}
That script is saved apart as setLang.php.
Thus, I start all my pages with :
session_start();
require($path."setLang.php");
$L = setLang($_SESSION['lang'],$_POST['L']);
I have put the complete header code in a separate file, called immediately after the language check/setting
require($path."pagetop.php");
In pagetop.php I use the variable $L for the first time in a query, like this:
$item="content".$L;
$query1="SELECT ";
$query1.=$item;
$query1.=" FROM `structuretext` ";
$query1.="WHERE ";
$query1.="content_id='";
$query1.=$pageref;
$query1.="' ";
Now, when I load my index page, everything goes fine.
But, when I follow a link to another page, or even if I reload the index page, I receive the error message:
Could not connect to table: Field'contentcontentGB' not known in field list?
So instead of passing the value "GB" (or whatever of the 3 other language codes) as session variable, on the second occurrence it is the value "contentGB".
Has this problem to do with the fact that the main page is not passing the value correctly to the embedded "pagetop.php" subpage, or is it just the fact to run the function setLang() over and over again?
Has anyone an idea what's going wrong with this script, please?
Thx.
Notawiz
$item="content".$L;
This is a hard one to follow. You'll probably have to debug this the old fashioned way - take it apart and follow the values of each variable involved through the process. Find ways to make the whole thing simpler. And - Try putting some debugging messages into your code, and see if the values are as you expect them to be.
I sometimes use this:
$debugmode=true;
function debug($x){
global $debugmode;
if($debugmode){print("<BR><b>".$x."</b>");}
}
debug("L is equal to".$L);
debug("query1 is ".$query1);
etc.
and the pre_print_r function is handy too:
function pre_print_r($var){
print("<pre>");
print_r($var);
print("</pre>");
}
About the $item="content".$L;
In the table, I have contentGB, contentF, contentD and contentSP.
Since I don't know in advance the language of the user, I have to use a variable to build the query on the mysql database.
All the code of the site worked just fine as long as I defined the language this way:
if ($L!= "") {
$L = $L;
}
else {
$L = "GB"; // the default
}
But I was not able to pass the value of $L to following pages.
So I thought storing this value in a session variable.
And since then it doesn't work any longer.
The funny thing is that I put it apart for debugging, as you advise, and on page load I get the right result ($item = contentGB for example), but as soon as I hit reload, I get contentcontentGB?
That's what I don't understand, why on the second run the script passes the value "contentGB" to the session variable instead of just "GB" (or "F" or "D" or "SP").
Any idea why?
Regards
Notawiz
Indeed, session is started on each page.
Not on the embedded files (the ones called by the require function), but that seems logical, since these "required" code snippets normally re-use the values of the main page that "required" them.
Just to make sure, I even tried to add a session start on those included/required bits, but it didn't change a thing.
And oh yes, I tried also to paste the whole script in a same page with other code using my $L variable. And then I was able to hit reload as many times as I wanted, the values stayed good.
Could it have something to do with the fact that the script is saved apart in another folder?
and you're right you don't need the session_start in the embedded files, just once in the top of the actual page.
hmmm, when I look at that code it seems that the function is just confusing itself. It seems way too comlicated for what it is doing.
Let me see if I can simplify it and then we can go from there.
httpwebwitch also thinks it is too complicated!
But what I tried to do is this:
I have a free area (no login) and a restricted area (login). So sometimes I will have a value for $_SESSION['lang'], other times I will not.
The display language can also be modified by a form.
So sometimes I have a value for $_POST['L'], other times I will not.
If session and post are equal, I unset post to keep session.
If they are different, I must override the value of session so that the user keeps the choice he made with the form.
Of course the post value will win when there is no known session value yet.
And when no information is available, the default is GB (for english).
Complicated? As far as languages are concerned, we here in Europe are used to complicated communication problems. And I just stick to 4, could have been more than 10 by now, with Polish and soon Turkish etc.
$allowedlangs = array('GB','F','D','SP');
if (isset($_POST['L']) && in_array($_POST['L'], $allowedlangs)) {
$_SESSION['lang'] = $_POST['L'];
} else if (!isset($_SESSION['lang']) ¦¦!in_array($_SESSION['lang'], $allowedlangs)) {
$_SESSION['lang'] = 'GB';
} - sets an array of allowed languages
- tests for the posted value first because it will always override the session value
- if no post value it tests for the existence of the session value
- if no session value it sets the default language
make sure you replace the '¦' character with a real pipe character as the WebmasterWorld software breaks pipes.