有时会话变量停止工作 [英] Sometimes Session variables stop working

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

问题描述

我已经吃过两次了.突然之间,我的登录系统停止工作,通过调试我发现 $_SESSION 变量在登录过程中无法生存.然后,在没有明显原因的情况下,它恢复工作.流程如下:

I've had this twice now. Out of the blue, my log-in system stops working, and by debugging I find out the $_SESSION variable does not survive the log-in process. Then, without an obvious cause, it resumes working. Here's the flow:

  1. 用户登录index.html,表单提交到login.php
  2. login.php 执行基本的健全性、isset 和空检查,然后使用数据库检查凭据.如果电子邮件地址和密码正确(即存在于数据库中),将它们放入 $_SESSION 变量并将用户重定向到 home.php.
  3. home.php 检索 $_SESSION 变量.这里失败了.
  1. User logs in at index.html, form submits to login.php;
  2. login.php does basic sanity, isset and empty checks, then checks the credentials with the database. If the email address and password are correct (i.e., exist in the database) put them in the $_SESSION variable and redirect user to home.php.
  3. home.php retrieves the $_SESSION variables. Here it fails.

第二次(几分钟前)我阅读了更多关于它的信息,并发现了一个我上次没有阅读过的论坛帖子(当会话变量再次工作时我停止阅读它),它说你需要有 而不是 session_start(); 之前.我试过了,没想到它会起作用,但是当我登录时,直接在更改后(这是我更改 AFAIK 的唯一内容),它起作用了.原因找到了吗?让我们在将 改回 后检查一下.它仍然有效.造成这种情况的原因是什么,我该如何预防(或者,如果无法预防,请检测发生了什么)?

The second time (a few minutes ago) I read more about it and found a forum thread I hadn't read the previous time it happened (I stopped reading about it when session variables worked again) which said you need to have <?php instead of <? before session_start();. I tried it, not expecting it to work, but when I logged in, directly after changing that (and that was the only thing I changed AFAIK) it worked. Cause found? Let's check after changing <?php back to <?. It still works. What can be the cause of this and how can I prevent it (or, if it can't be prevented, detect what's going on)?

有趣的事情:我有一个小的实用函数来检查用户是否登录:

Something interesting: I've got a small utility function to check if the user is logged in:

function assertUserLogin() {
        try {
            $user = new User($_SESSION['email'], $_SESSION['pwd']);
        } catch(Exception $ex){
            writeToLog("Exception: " . $ex->getMessage());
            header("Location: http://www.korilu.nl/maurits/anw?requested:" . $_SERVER["REQUEST_URI"]);
        }
        writeToLog($user->email . " logged in\n");
        
        return $user;
    }

所以我可以这样做:

<?
    session_start();
    $user = assertUserLogin();
?>

在每个页面上,用户都需要登录.这里有趣的是,如果失败(如上所述),它会调用我的函数 writeToLog() (log() 已经被 PHP 标准库采用):

On every page the user needs to be logged in. The interesting thing here is, that if it fails (as described above), it calls my function writeToLog() (log() is already taken by the PHP standard library):

function writeToLog($string) {
        $log = fopen("log.txt", "w");
        fwrite($log, $string);
        fclose($log);
    }

这很简单.但是日志仍然是空的.(我确定函数 writeToLog() 被调用,因为我被重定向到 http://www.korilu.nl/maurits/anw?requested:/maurits/anw/home.php.assertUserLogin() 函数是唯一能做到这一点的地方.)

which is pretty simple. But the log remains empty. (I am sure the function writeToLog() gets called, because I get redirected to http://www.korilu.nl/maurits/anw?requested:/maurits/anw/home.php. The assertUserLogin() function is the only place that does that.)

推荐答案

我发现这是一个特定于浏览器的问题.我认为这是由谷歌浏览器引起的,因为一旦我使用移动版 Safari 或 Mozilla Firefox 来测试会话,它就会消失.虽然在高级设置中我可以看到 PHPSESSID cookie,但它没有获取会话.

I found out it is a browser-specific issue. It was caused by Google Chrome, I think, because it vanishes as soon as I use mobile Safari or Mozilla Firefox to test the Sessions. Although in the advanced settings I could see the PHPSESSID cookie, it didn't pickup the session.

我错了.Mozilla 也开始放弃会话.在我删除会话 (session_destroy()) 后,它再次工作.所以我的猜测是,在服务器上的会话过期后,浏览器仍然拥有 PHPSESSID cookie.如果它把它发送到服务器,服务器找不到会话,只是在 $_SESSION 中放置一个空数组,让我一无所知.我希望这可以帮助遇到同样问题的人.

I was wrong. Mozilla started to drop the session too. After I deleted the session (session_destroy()) it worked again though. So my guess is that after the session expires on the server, the browser still has the PHPSESSID cookie. If it sends that to the server, the server can't find the session and just puts an empty array in $_SESSION, leaving me clueless. I hope this helps somebody having the same problem.

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

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