PHP的会话变量丢失 [英] php session variable lost

查看:82
本文介绍了PHP的会话变量丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发机器上使用php5和apache2.生产服务器显然很相似.

I work with php5 and apache2 on my development machine. The production server is apparently similar.

我有一个脚本set_language.php,可根据所选语言创建会话变量:

I have a script set_language.php that creates a session variable according to the language chosen:

<?php
session_start();
$back = $_SERVER['HTTP_REFERER'];

    if (isset($_GET['lang'])) {
            if ($_GET['lang'] == 'fr')
            $_SESSION['lang'] = 'fr';
        else if ($_GET['lang'] == 'en')
            $_SESSION['lang'] = 'en';
    }
    header( "Location: $back" ) ;
    exit();
?>

然后,我在所有页面上都包含以下代码:

Then I include the code below on all pages:

session_start();
if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en') {
    require('lang_en.php');
}
else if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'fr') {
require('lang_fr.php');
}

我可以通过以下方式获得与语言相关的变量:

Than I can get language-dependent variables with:

echo $lang[sometexttotranslate];

在开发服务器上,它可以按预期工作.我单击链接以设置语言,并且我的会话变量包含lang = en或fr

On the development server it works as expected. I click on the link to set the language and my session variables contain lang=en or fr

在生产服务器上:

$ _ SESSION ['lang'] = dn dr (应为en或fr)

$_SESSION['lang'] = dn or dr (should be en or fr)

echo $ lang [sometexttotranslate] = d (应为翻译后的文本)

echo $lang[sometexttotranslate] = d (should be a translated text)

您知道"d"的来源是什么吗?

Any idea where the "d"'s come from?

这不是重定向/标头/session_start()问题.

This is not a redirection/header/session_start() problem.

print_r($ _ SESSION)首次加载:

print_r($_SESSION) on first load:

数组([lang] => fr)

print_r($ _ SESSION):

print_r($_SESSION) after a click on the english link (set_lang.php above):

数组([lang] => dn)

print_r($ _ SESSION):

print_r($_SESSION) after a click on the french link (set_lang.php above):

数组([lang] => dr)

print_r($ _ SESSION)

print_r($_SESSION) after login:

数组([lang] => dr [valid] => 1 [pseudo] => GYC [uid] => 3)

推荐答案

现在这件事了,

print_r($_SESSION) after a click on the english link (set_lang.php above):

Array ( [lang] => dn )

您的问题从set_lang.php开始. 也许会话是从包含的文件开始的,这不应该发生!

your problem starts here in set_lang.php . Maybe a session is started from a file that is included and this should not happen!

如果不包括set_lang,请确保那里存在一个start_session,也将一个print_r放在那里,您甚至可以比较会话ID:

If set_lang is not included make sure a start_session exist there, also put a print_r there too, you could even compare the session ids too:

让我们说这是index.php

let's say this is index.php

<?php
session_start();  
?>
<a href='set_lang.php?sess=<?PHP echo session_id();?>'>lang</a>

和这个set_lang.php

and this set_lang.php

<?PHP  
session_start(); 
echo 'this is session id from index.php: ',$_GET['sess'],
'and this is session id in set_lang.php:',session_id(),
'and this is the print_r:<br><pre>';
print_r($_SESSION);
?>

您应该看到相同的会话ID.

you should see the same session id.

尽管所有这些都可能对您有所帮助,但我将更进一步,请使用单个入口点,不要像这样直接调用php脚本

But while all these might help you I will take it a step further, use single entry point, do not call php script directly like this

<a href=somescript.php>link</a>

(除非您正在做一些ajax),否则您总是像这样通过index.php调用一些php脚本:

(unless you are doing some ajax) instead you always call some php script through index.php like this:

<a href=index.php?target=somescript.php>link</a>

这样,您将只在index.php中放置一次会话,登录检查也可以在index.php中进行.

This way you will have the session start placed only once in the index.php, the login check can be done in index.php too.

=======================编辑更多信息已添加==================== ===================== 快速的Google搜索.发现了这个小型入门教程 http://www.renownedmedia.com/blog/php-navigation-system-using-single-entry-point/可能会有更好的方法.

=======================Edit More Info Added========================================= A fast google search.. found this small single entry tutorial http://www.renownedmedia.com/blog/php-navigation-system-using-single-entry-point/ there could be better ones.

更好的是切换到mvc框架(包括单入口哲学),但是因为学习曲线至少从单入口开始才更加陡峭.

===========重新编辑更多信息再次添加============================ ============ 不错的选择是再次从您的项目上下文中复制此问题,在新创建的文件中从项目中获取尽可能少的代码,然后尝试再次创建问题.使其尽可能简单,只需再次尝试创建错误

============ReEdit more info added again========================================== A good option then would be to replicate this problem again but out of your project context, take as less as possible code from your project in new created files and try to create the problem again. keep it as simple as possible just try to create the error again

这篇关于PHP的会话变量丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆