dongtan3306 2014-11-05 02:10
浏览 40
已采纳

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

I am trying to do a login for php and sql server by different UserType I've tried to do a login without the UserType and i can login. But i have no idea how to do a login based on UserType. Anyways, this is what i have tried :

UserType :

  • User
  • Superior
  • Admin

login_action.php (w/o UserType)

<?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 (with 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条回答 默认 最新

  • dragon8837 2014-11-05 02:25
    关注

    Its okay, I've got it worked out. I just forgot to write this line :

    $_SESSION['valid_user'] = true;
    

    And as Fred -ii- suggested, I've added the exit(); after each of the header

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作