创建 PHP 会话变量会挂起我的浏览器
我最近才从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里只是做一个猜测。
您的位置标头后面需要有一个
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 theexit();
though.