dongzhang6544 2016-02-27 22:29
浏览 91
已采纳

对于多语言网站,PHP无法使用$ _Session检索正确的语言

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>
  • 写回答

2条回答 默认 最新

  • duanpo1498 2016-02-27 22:50
    关注
    1. You should always call session_start() before working with $_SESSION. You miss that in the switch_lang.php. Look here.
    2. Could you format your code pls, its hard to read it this one line form.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化