基于 $_SESSION 的语言选择无法正常工作
$_SESSION based language selection is not working properly
我的网站有两种语言:英语和俄语。这些语言的数据库名称是 EN 和 RU。
我使用旧的 php+smarty 脚本。此脚本的默认语言选择代码如下:
if(!isset($_SESSION)){
define('LANGUAGE_ID', 'EN');
} else if ($_SESSION['language'] == '')
{
define('LANGUAGE_ID', 'EN');
}
当我想看俄语页面时,我会像这样访问带有语言短语 (?language=RU) 的任何页面:
http://www.example.com/index.php?language=RU
但是这些页面首先不会以俄语加载。当我单击另一个 link 或刷新页面时,页面以俄语加载。保存 cookie 后,我可以在第一次访问时看到俄语页面。但是如果我删除浏览器中的 cookie,那么我在第一次访问时就看不到俄语了。
我尝试了很多组合,但找不到任何解决方案。你有什么主意吗?
非常感谢...
编辑:
我在 main.class.php 中找到了一些代码:
function __construct($dbh,$smartyBuild) {
$this->dbh = $dbh;
$this->sitevar = @$smartyBuild->FetchSiteVar();
$this->smartybuild = @$smartyBuild;
if($_REQUEST['language'] !='')
{
$_SESSION['language'] = $_REQUEST['language'];
}
else
{
$langaugeAlready = mysql_fetch_array(mysql_query("select value from ".TABLE_PREFIX."sitevars where array_key = 'default_language_feed'"));
if($_SESSION['language'] == '')
{
$_SESSION['language'] = $langaugeAlready['value'];
}
}
if($_SESSION['language'] !='' )
{
define('LANGUAGE_ID', $_SESSION['language']);
}
else
{
define('LANGUAGE_ID', 'EN');
$_SESSION['language'] = 'EN';
}
}
问题与这些代码有关吗?
就像我说的,如果没有所有代码,我们只能猜测问题出在哪里,但是,这里...
您似乎只检查语言的 $_SESSION 变量而不是 $_GET 变量(从 URL 获取语言)。因此,该值只会在您刷新页面后发生变化。
试试这个。我假设您的意图是将英语显示为默认值,如果它在 url 中定义,则仅显示俄语,但一旦定义为保留该语言,直到它再次放入 URL。
//start a session. must be called first on all pages you want it to work on
session_start();
//first check if there's a new language coming from the URL
if(isset($_GET['language']))
{
// if we have a new language setting from the URL, check which one and save it in the session.
// we check it is EN or RO before saving into the session since I don't know what you're using it for later. eg part of a DB query which would be a security risk if it is anything other than EN or RO.
if($_GET['language'] == 'EN')
{
$_SESSION['language'] = 'EN';
}
if($_GET['language'] == 'RO')
{
$_SESSION['language'] = 'RO';
}
}
//now check the session variable, which will have been updated above if changed
if(isset($_SESSION['language']))
{
// already have a language saved, so let's use it
define('LANGUAGE_ID', $_SESSION['language']);
}
else
{
// no language from URL and no language saved, so default to english
define('LANGUAGE_ID', 'EN');
}
我的网站有两种语言:英语和俄语。这些语言的数据库名称是 EN 和 RU。
我使用旧的 php+smarty 脚本。此脚本的默认语言选择代码如下:
if(!isset($_SESSION)){
define('LANGUAGE_ID', 'EN');
} else if ($_SESSION['language'] == '')
{
define('LANGUAGE_ID', 'EN');
}
当我想看俄语页面时,我会像这样访问带有语言短语 (?language=RU) 的任何页面:
http://www.example.com/index.php?language=RU
但是这些页面首先不会以俄语加载。当我单击另一个 link 或刷新页面时,页面以俄语加载。保存 cookie 后,我可以在第一次访问时看到俄语页面。但是如果我删除浏览器中的 cookie,那么我在第一次访问时就看不到俄语了。
我尝试了很多组合,但找不到任何解决方案。你有什么主意吗?
非常感谢...
编辑:
我在 main.class.php 中找到了一些代码:
function __construct($dbh,$smartyBuild) {
$this->dbh = $dbh;
$this->sitevar = @$smartyBuild->FetchSiteVar();
$this->smartybuild = @$smartyBuild;
if($_REQUEST['language'] !='')
{
$_SESSION['language'] = $_REQUEST['language'];
}
else
{
$langaugeAlready = mysql_fetch_array(mysql_query("select value from ".TABLE_PREFIX."sitevars where array_key = 'default_language_feed'"));
if($_SESSION['language'] == '')
{
$_SESSION['language'] = $langaugeAlready['value'];
}
}
if($_SESSION['language'] !='' )
{
define('LANGUAGE_ID', $_SESSION['language']);
}
else
{
define('LANGUAGE_ID', 'EN');
$_SESSION['language'] = 'EN';
}
}
问题与这些代码有关吗?
就像我说的,如果没有所有代码,我们只能猜测问题出在哪里,但是,这里...
您似乎只检查语言的 $_SESSION 变量而不是 $_GET 变量(从 URL 获取语言)。因此,该值只会在您刷新页面后发生变化。
试试这个。我假设您的意图是将英语显示为默认值,如果它在 url 中定义,则仅显示俄语,但一旦定义为保留该语言,直到它再次放入 URL。
//start a session. must be called first on all pages you want it to work on
session_start();
//first check if there's a new language coming from the URL
if(isset($_GET['language']))
{
// if we have a new language setting from the URL, check which one and save it in the session.
// we check it is EN or RO before saving into the session since I don't know what you're using it for later. eg part of a DB query which would be a security risk if it is anything other than EN or RO.
if($_GET['language'] == 'EN')
{
$_SESSION['language'] = 'EN';
}
if($_GET['language'] == 'RO')
{
$_SESSION['language'] = 'RO';
}
}
//now check the session variable, which will have been updated above if changed
if(isset($_SESSION['language']))
{
// already have a language saved, so let's use it
define('LANGUAGE_ID', $_SESSION['language']);
}
else
{
// no language from URL and no language saved, so default to english
define('LANGUAGE_ID', 'EN');
}