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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助