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

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题