使用PHP和SQL Server登录不同级别的用户

时间:2014-11-05 02:10:41

标签: php sql sql-server login

我正在尝试通过不同的UserType登录php和sql server我尝试在没有UserType的情况下登录,我可以登录。但我不知道如何根据UserType进行登录。无论如何,这是我尝试过的:

UserType

  • 用户
  • 高级
  • 管理

login_action.php (没有用户类型)

<?php
session_start();

require_once 'connection.php';

$EmployeeId = $_POST['EmployeeId'];
$Password  = $_POST['Password'];

$tsql = "SELECT * FROM LOGIN WHERE EmployeeId='$EmployeeId' AND Password='$Password'";
$result = sqlsrv_query( $conn, $tsql, $params, array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

$num = sqlsrv_num_rows($result);
if($num==1){
    header('Location: user.php');
    $_SESSION['valid_user'] = true;
    $_SESSION['EmployeeId'] = $EmployeeId;
    die();}
else
    header('Location: login.php');
    die();      
?>

login_action.php (使用UserType)

<?php
session_start();

require_once 'connection.php';

$EmployeeId = $_POST['EmployeeId'];
$Password  = $_POST['Password'];
$UserType = $_POST['UserType'];

$tsql = "SELECT * FROM LOGIN WHERE EmployeeId='$EmployeeId' AND Password='$Password' AND UserType='$UserType'";
$result = sqlsrv_query( $conn, $tsql,$params , array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

$num = sqlsrv_num_rows($result);
if($num==1){
    $_SESSION['EmployeeId']=$EmployeeId;
    $_SESSION['UserType']=$UserType;
    if($UserType=="USER")
    {
        header("Location:user.php");
    }
    else if ($UserType=="SUPERIOR")
    {
        header("Location:superior.php");
    }
    else if ($UserType=="ADMIN")
    {
        header("Location:admin.php");
    }
    else 
    die("Not a valid User Type");

    }
    else
    header('Location: login.php');
?>

1 个答案:

答案 0 :(得分:1)

没关系,我已经解决了。我只是忘了写这一行:

$_SESSION['valid_user'] = true;

正如Fred -ii-建议的那样,我已经添加了exit();在每个标题之后