2017-07-26 43 views
-2

我有一个登录页面。管理员A用于主分支和其他管理员B,C以及分支B,C和D.管理员A可以登录到所有分支,而管理员B,C和D只能登录到他们自己的分支。但现在我可以做的是所有的管理员只能登录到他们自己的分支。我如何做到这一点,管理员A也可以登录到分支B,C和D?用两种不同的条件登录表格

这是我的代码。在数据库中的登录表

if($_POST['username'] && $_POST['password'] && $_POST['txtbranch']) { 
$username = $_POST['username']; 
$password = $_POST['password']; 
$txtbranch = $_POST['txtbranch']; 

$hash_pass = $password; 

$query = mysqli_query("select * from sysfile where username='".$username."' and password='".$hash_pass."' and branchid='".$txtbranch."' limit 1"); 

$count_user = mysqli_num_rows($query); 
if($count_user==1) 
{ 
    $row = mysqli_fetch_array($query); 
    $_SESSION['userid'] = $row['userid']; 
    $_SESSION['username'] = $row['username']; 
    $_SESSION['pin'] = $row['pin']; 
    $_SESSION['branchid'] = $_POST['txtbranch']; 
    header("location:dashboard.php?success=1"); 
}else{       
     $error = 'Incorrect Username, Password and Branch.';  
    } 
} 
+0

'联系A'是一个用户名?你对SQL注入开放,应该有散列密码。 – chris85

+1

你实际上哈希?改变一个变量名不是哈希.. – chris85

+0

这听起来可能让你灰心。请使用准备好的声明并学习如何在推进之前对您的密码进行哈希处理:http://php.net/manual/en/mysqli.prepare.php,http://php.net/manual/en/pdo.prepared-statements .php和http://php.net/manual/en/function.password-hash.php,http://php.net/manual/en/function.password-verify.php –

回答

1
  • 附加类型字段
  • 然后添加所有管理和设置型等管理员A = 1,而B,C后,d = 2
  • 现在设定的条件在登录页面

if ($type == 1) { 
    //Admin A login 
} else { 
    // Another Admin Login 
} 
0

我会去,并添加对于用户的权限

┌──────┬──────┐ 
│userid│branch│ 
├──────┼──────┤ 
│ 1 │ A │ 
│ 1 │ B │ 
│ 1 │ C │ 
│ 1 │ D │ 
│ 2 │ B │ 
│ 2 │ C │ 
│ 2 │ D │ 
└──────┴──────┘ 

表然后你就可以发出类似下面

SELECT * 
    FROM users, userrights 
    WHERE users.userid=userrights.userid 
     AND [email protected] 
     AND [email protected]_pass 
    LIMIT 1 

的SQL语句该SQL查询选择所有的列(更好地选择列明确! SELECT userid, username, pin, branchid FROM ...)从加入的表usersuserrights(为了清楚起见将表格重新命名)。后者保存用户对表格的权利。过滤和限制为您的原始查询,略有不同,我已使用SQL参数来防止注入。

随着该查询,你可以让你的

$count_user = mysqli_num_rows($query); 
if($count_user==1) 
{ 
[...] 
0

更换branch_id与作用,以便它可以很容易理解其他人也。 您必须在数据库命名中插入一个标志作为角色。 如果角色是1 - 你的情况(A - 主要分支) 如果角色是2 - 你的情况(B,C,d - 其他分支)

假设你的数据库内在张力结构会像

id | username | password | role | 
--------------------------------- 
1 | A  | 123  | 1 
--------------------------------- 
2 | B  | 1111  | 2 
--------------------------------- 
3 | C  | 2323  | 2 
--------------------------------- 
4 | D  | 1782  | 2 

我不喜欢纯文本密码存储在数据库中,这仅仅是为了举例。 你的代码看起来像这样:

if($_POST['username'] && $_POST['password'] && $_POST['txtbranch']) 
{ 
$username = $_POST['username']; 
$password = $_POST['password']; 
$txtbranch = $_POST['txtbranch']; 

$hash_pass = $password; 

$query = mysqli_query("select * from sysfile where username='".$username."' and password='".$hash_pass."' and branchid='".$txtbranch."' limit 1"); 

$count_user = mysqli_num_rows($query); 
if($count_user==1) 
{ 
    $row = mysqli_fetch_array($query); 
    $role = $row['role']; 
    if($role == 1) 
    { 
     //that is A then set the session and redirect to A dashboard 
    } 
    else if($role ==2) 
    { 
     //that is B,C and D then set the session and redirect to there respective dashbaord 
    } 
    else 
    { 
     //whatever action you want 
    } 
} 
else 
{       
     $error = 'Incorrect Username, Password and Branch.';  
} 
} 
+0

我会先试试这个。 – user1120

0

我已经解决我的问题,这是我最后的代码。

if($_POST['SUBMIT']=='Sign In') 
{ 
if($_POST['username'] && $_POST['password'] && $_POST['txtbranch']) 
{ 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $txtbranch = $_POST['txtbranch']; 

    $query_callbranch=mysqli_query("SELECT * FROM sysfile WHERE USERNAME='$username' ")or die(mysqli_error()); 
    while($rowcallbranch=mysqli_fetch_array($query_callbranch)) 
    { 
     $pin = $rowcallbranch['pin']; 

     if($pin == '9' && $username == 'admin') 
     { 
      $query = mysqli_query("select * from sysfile where username='".$username."' and password='".$password."' limit 1"); 

      $count_user = mysqli_num_rows($query); 
      if($count_user==1) 
      { 
       $row = mysqli_fetch_array($query); 

       $_SESSION['userid'] = $row['userid']; 
       $_SESSION['username'] = $row['username']; 
       $_SESSION['pin'] = $row['pin']; 
       $_SESSION['branchid'] = $_POST['txtbranch']; 
       header("location:dashboard.php?success=1"); 
      }else{ 
       $error = 'Incorrect Username dan Password.';  
      } 
     } 

     if($pin == '0') 
     { 
      $query = mysqli_query("select * from sysfile where username='".$username."' and password='".$hash_pass."' and branchid='".$txtbranch."' limit 1"); 

      $count_user = mysqli_num_rows($query); 
      if($count_user==1) 
      { 
       $row = mysqli_fetch_array($query); 

       $_SESSION['userid'] = $row['userid']; 
       $_SESSION['username'] = $row['username']; 
       $_SESSION['pin'] = $row['pin']; 
       $_SESSION['branchid'] = $_POST['txtbranch']; 
       header("location:dashboard.php?success=1"); 
      }else{ 
        $error = 'Incorrect Username, Password and Branch.';  
      } 
     }    
    } 
} 

}