You are here: Unexplanable SESSION variable change? « All PHP « IT news, forums, messages
Unexplanable SESSION variable change?

Posted by Rik on 09/27/87 11:29

Hi,

I'm trying to get a website for a friend readable in different languages.
Maybe not the best way, but I've chosen to add all text to a MySQL table,
from wich i get the data:

$lng_query = @mysql_query("SELECT tag, ".$_SESSION['lang']." FROM
mat_txt_lang");
if(mysql_affected_rows() > 0)
{
while($row = mysql_fetch_row($lng_query))
{
$vartext[$row[0]] = $row[1];
}
}

Indeed, working with with $_SESSION. Included in the header of the document:

if (isset($lng) && $lng != ""){
if ($lng=="nl" OR $lng=="de" OR $lng=="en"){
$_SESSION['lang']=$lng;
}
}
if (!isset($_SESSION['lang'])) {
// code from Daniel "elixon" Sevcik found on php.net
function chooseLang($availableLangs) {
$pref=array();
foreach(split(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $langpref) {
if (preg_match('/^([a-z]+).*?(?:;q=([0-9.]+))?/i', $langpref.';q=1.0',
$split)) {
$pref[sprintf("%f%d", $split[2],
rand(0,9999))]=strtolower($split[1]);
}
}
krsort($pref);
return array_shift(array_merge(array_intersect($pref, $availableLangs),
$availableLangs));
}
$_SESSION['lang'] = chooseLang(array('nl', 'en', 'de'));
}

Yet to be included is language preference from registered logged in users,
but as logged in / not logged in ratio would be something in the order of
10% / 90%, this part of the code should work.

On the bottom of the page 3 flags which add ?lng=en(or nl or de) to the url
to change the language which should set the session variable, so in the rest
of the document i can use $vartext['tagname'] for the text. Bonus is that
the user still sees the current page, only the language has changed.

The problem is: most of the time is works, but if I click an URL repeatedly,
sometimes the language changes "automagically". This seems to have nothing
to do with the preferred language of the browser, I replaced that code to
get a static standard language like $_SESSION['lang'] = "en", and the
problem persists. No where else in the script the variable $_SESSION['lang']
or $lang is used or modified.

As said: most of the time is works, after repeatedly clicking an url it
seems to change to another language. Could this be a problem of an incorrect
loaded cookie? Firefox, MSIE and Opera all have the same problem, so you'd
think it would be serversided, but where?

Thanks in advance,

Rik

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация