将值从 mysql 分配给会话变量和条件语句的问题


Issue with assigning value from mysql to session variable and conditional statements

真的可以在这方面使用一些帮助。 我在从分配为会话变量的数据库中获取值以及我的 checklogin.php 脚本中的条件语句时遇到问题。

下面是名为 checklogin.php 的脚本的代码。 这是基本的原版脚本,它从基本的原版登录表单接收用户名和密码,并将其与数据库进行比较以确保其准确无误,然后通过分配用户的用户名会话变量来登录用户,然后从数据库中检索用户的角色并将其分配给会话变量。 然后,根据其用户角色循环使用条件语句,以便将审批者或管理员重定向到显示所有 Office 文件计划以供审阅的窗体,并将具有用户角色的人员定向到仅显示用户创建的 Office 文件计划的窗体。

所以我的问题是只有用户名被分配给会话变量。 我的代码有问题,以至于用户角色没有分配给会话变量。 我还有其他脚本可以从后端数据库中获取值并将它们分配给会话变量。 所以我绝对困惑为什么它在这里不起作用。 由于我的条件语句同时依赖于用户名和用户角色,因此无法正确解析。 条件语句正在解析,以便所有用户(无论角色如何)都将被定向到 ApproverPlanSelect.php 脚本。

接下来,我知道mysql_query已被弃用,但我的组织使用早期版本,它仍然可以运行。 因此,我想恭敬地要求人们专注于帮助我调试会话变量和条件语句问题,而不是对已弃用的语句留下一句"路过"评论。

我真的非常感谢任何建议。 我已经坚持了几个星期了,它踢了我的屁股。

<?php
/*checklogin.php....this script receives the username and password from a basic form*/
session_start();
ob_start();
$host='localhost'; /*host name*/
$username='bigbear1';/*fake username*/
$password='fakepw123';/*fake password*/
$db_name='TheDatabase';/*fake database name*/
$tbl_name='members';/*common table name for users*/
/*Connect to server and select database*/
mysql_connect($host,$username,$password) or die("Cannot connect");
mysql_select_db($db_name)or die("Cannot select DB");
/*Define $current_user_name and $mypassword*/
$current_user_name=$_POST['current_user_name'];
$mypassword=$_POST['mypassword'];
/*SQL Injection countermeasures*/
$current_user_name = stripslashes($current_user_name);
$mypassword = stripslashes($mypassword);
$current_user_name = mysql_real_escape_string($current_user_name);
$mypassword = mysql_real_escape_string($mypassword);

$sql = "SELECT * FROM $tbl_name WHERE username='$current_user_name' AND password='$mypassword'";
$result = mysql_query($sql);
/*next count the number of rows generated by the previous query*/
$count=mysql_num_rows($result);
/*if the username and password are correct at least one table row will be counted*/
if ($count >= 1)
{
/*this runs a query and determines the user's role (Approver, Administrator, User) and assigns that role to $current_user_role*/
$query1="SELECT * FROM members WHERE username = '$current_user_name'";
$result1 = mysql_query($query1);
while ($row = mysql_fetch_array($result1));
{
$current_user_role = $row1['role'];
}   
/*If the the role is "User", that role is assigned to a session variable and the user is redirected to the NonApproverPlanSelect form where the user can see only the file he/she created*/
if ($current_user_role=='User')
  {
         $_SESSION['current_user_name'] = $current_user_name;
     $_SESSION['current_user_role'] = $current_user_role;
     header("location:NonApproverPlanSelect.php");
      }
/*If the role is not "User" (i.e. Approver or Administrator), the role is assigned to a session variable and the user is redirected to the ApproverPlanSelect form that displays ALL office file plans regardless who created them.*/

    else
      {
         $_SESSION['current_user_name'] = $current_user_name;
     $_SESSION['current_user_role'] = $current_user_role;
     header("location:ApproverPlanSelect.php");
      }
}
else
{
header(location:bad_login.php");
}
ob_end_flush();
?>

下面是 NonApproverPlanSelect.php 和 ApproverPlanSelect.php 脚本顶部的 session_start) 语句。 用户名显示正确,但用户角色未正确显示。

<?php
session_start();
if(!isset($_SESSION['current_user_name'])) {
header('Location:view.php');
}
echo "Welcome " . $_SESSION['current_user_name'] . ".<br>";
echo "You are logged in as " . $_SESSION['current_user_role'] . ".<br>"; 
echo 'href="logout.php"><span>Logout</span</a><li>';
?>

好的....我想通了,正在发布这个以防它对某人有帮助。 我不需要使用 while 语句,只需通过 mysql_fetch_array 将查询结果传递到变量中,即 $row=mysql_fetch_array(result1),然后将数据库中的列名分配给 $row['role']。 希望这对未来的一些可怜的灵魂有所帮助,哈哈