简体   繁体   English

跨页面发出持久会话变量

[英]Issue persisting session variables across pages

First of all a little bit of background on my setup: 首先,我的设置需要一些背景知识:

  • I have a local domain name set up, projects.lumesse.com. 我设置了一个本地域名,projects.lumesse.com。
  • PHP version is 5.4.16 running on IIS 7. IIS 7上运行的PHP版本是5.4.16。
  • Testing in Chrome (latest stable build). 在Chrome中测试(最新的稳定版本)。

The problem is as follows: 问题如下:

I have a function called 'getVariable' as follows: 我有一个名为“ getVariable”的函数,如下所示:

function getVariable($name, $default = "") {
    if(isset($_POST[$name])) return $_POST[$name];
    if(isset($_GET[$name])) return $_GET[$name];
    if(isset($_SESSION[$name])) return $_SESSION[$name];
    return $default;
}

Going through this function line by line it returns the post variable if it exists, followed by the get variable, followed by the session variable followed by the default (if none of the others exist). 逐行浏览该函数,如果存在,则返回post变量,然后返回get变量,然后是session变量,然后是默认变量(如果不存在其他变量)。

In another include, which is included directly after the functions include, I have the following line: 在包含函数之后直接包含的另一个包含中,我有以下内容:

$_SESSION["Language"] = getVariable("Language", "FR");

This works fine if I put ?Language=DE - the site displays in German as expected. 如果我输入?Language = DE,这将很好地工作-该站点将按预期显示德语。 However from the line above I'd expect that the language is persisted if I strip off the querystring. 但是从上面的行中,我希望如果我剥离查询字符串,该语言将保留下来。

The first time this page is hit, with ?Language=DE, it should return the get variable. 第一次使用?Language = DE命中此页面时,它应该返回get变量。 The line above then sets the session variable. 然后,上面的行设置会话变量。 Any calls to this function after this would therefore return the session variable, right? 因此,在此之后对该函数的任何调用都将返回会话变量,对吗?

It is actually returning FR, the default, in the case that no language parameter exists, even if I've set the language beforehand. 即使没有预先设置语言,在没有语言参数的情况下,它实际上会返回默认值FR。

Any ideas on what I'm missing would be much appreciated. 关于我所缺少的任何想法将不胜感激。

Found the answer - I'm used to coding in classic ASP and .net which don't need a line to initialise the session. 找到了答案-我习惯于使用经典的ASP和.net进行编码,而无需一行即可初始化会话。 Solution was to put session_start() in the main includes file. 解决方案是将session_start()放在主包含文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM