创建 PHP 会话变量会挂起我的浏览器

发布于 2025-01-06 08:36:20 字数 1405 浏览 0 评论 0原文

我最近才从 ASP 的困境中走出来,并且在适应 PHP 的阳光方面遇到了困难。

我当前的问题在于一个简单的登录序列,在该序列中我创建了一个会话变量 - 该步骤导致我的浏览器挂起,然后行为不稳定。

从我的登录页面(A.php),登录表单被定向到 B.php(如下),它处理密码,创建会话变量,然后将用户重定向到另一个文件(C.php)。

为简洁起见,我只是假设登录成功。 B.php 包含以下内容:

<?php
session_start();
require "../scripts/base/toolbox.php";

fnProcessLogin();

function fnProcessLogin(){
    $passwd = strtoupper($_POST["passwd"]);
    if (strlen($passwd)==0)
    {
    $passwd=strtoupper($_SESSION['plpassword']);
    unset($_SESSION['plpassword']);
  } 
  try{
    $db = Database::getDB();
    $sql="SELECT securitylevel, staffID, staffname, stafflname, staffemail, iRoleID FROM staff WHERE staffpasswd=?;";
    $data = array($passwd);
    $query = $db->prepare($sql);
    $query->execute($data);
    if($query->rowCount()>0){
      $row = $query->fetch();
      $a=$passwd."|".$row['staffID']."|".$row['staffname']."|".$row['stafflname']."|".$row['staffemail']."|".$row['iRoleID'];
      $_SESSION['admin'] = $a;
      header('Location: C.php');
    }

 } 
  catch(PDOException $pe){
    echo "We are sorry, but we cannot complete this database operation.";
    file_put_contents('PDOerrors.txt',$pe->getMessage(),FILE_APPEND);
  }
} 

?>

如果我注释掉“$_SESSION['admin'] = $a;”行,重定向工作正常,但一旦我尝试创建该会话变量,我的浏览器就会挂起,直到最终转到 C.php,无法正确加载任何文件。后退按钮操作似乎使浏览器陷入无限循环。

这个穴居人做错了什么?

谢谢,

布莱恩。

I've only recently emerged from my ASP cave and am having trouble adjusting to the sunshine of PHP.

My current problem lies with a simple login sequence in which I create a session variable - that step causes my browser to hang and then act erratically.

From my login page (A.php) the login form is directed to B.php (below) which processes the password, creates the session variable and then redirects the user to another file (C.php).

For brevity, I'm just assuming the login is successful. B.php contains the following:

<?php
session_start();
require "../scripts/base/toolbox.php";

fnProcessLogin();

function fnProcessLogin(){
    $passwd = strtoupper($_POST["passwd"]);
    if (strlen($passwd)==0)
    {
    $passwd=strtoupper($_SESSION['plpassword']);
    unset($_SESSION['plpassword']);
  } 
  try{
    $db = Database::getDB();
    $sql="SELECT securitylevel, staffID, staffname, stafflname, staffemail, iRoleID FROM staff WHERE staffpasswd=?;";
    $data = array($passwd);
    $query = $db->prepare($sql);
    $query->execute($data);
    if($query->rowCount()>0){
      $row = $query->fetch();
      $a=$passwd."|".$row['staffID']."|".$row['staffname']."|".$row['stafflname']."|".$row['staffemail']."|".$row['iRoleID'];
      $_SESSION['admin'] = $a;
      header('Location: C.php');
    }

 } 
  catch(PDOException $pe){
    echo "We are sorry, but we cannot complete this database operation.";
    file_put_contents('PDOerrors.txt',$pe->getMessage(),FILE_APPEND);
  }
} 

?>

If I comment out the "$_SESSION['admin'] = $a;" line, the redirection works fine, but as soon as I try to create that session variable, my browser hangs, until eventually going to C.php where it fails to load any files properly. Back button action seems to place the browser in an endless loop.

What's this caveman doing wrong?

Thanks,

Brian.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2025-01-13 08:36:20

我在这里只是做一个猜测。

您的位置标头后面需要有一个 exit(); 。如果在重定向后输出更多数据(例如尝试设置 cookie 的会话),重定向将会失败。

测试一下,看看会发生什么。

您还可以在会话分配后尝试 session_write_close(),以强制在尝试重定向之前完成所有会话相关数据。不过,我仍然强烈推荐 exit();

I'm just making a guess here.

You need to have an exit(); after your location header. If more data is being output after your redirect (like perhaps, a session trying to set a cookie) the redirect will fail.

Give it a test and see what happens.

You could also try a session_write_close() after your session assignment to force all session related data to be finished before attempting the redirect. I still highly recommend the exit(); though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文