簡體   English   中英

在php中區分管理員登錄名和用戶登錄名

[英]Differentiate between admin login and user login in php

我是php的新手,我正在嘗試創建一個登錄頁面表單

<form action="welcome.php" method="post">
Login: <input type="text" name="name"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login as admin user">
<input type="submit" value="Logins as Student">

我要區分,哪個提交按鈕用戶單擊並定向到他的相應頁面

即,如果他單擊以用戶身份登錄,則應轉發到adminLogin.php或Vise Versa。

只需將Status Column添加到數據庫中,例如Status = 1和Status = 0,如果找到Status 1,則找到admin,然后找到Status 0,然后找到user。

更好的方法是在數據庫中添加一列以區分用戶類型,並根據輸入的憑據在登錄時獲取數據並進行相應的重定向。

在注冊時,請區分管理員登錄名和用戶登錄名。 然后,當登錄過程開始時,您可以輕松確定是用戶登錄還是管理員登錄。然后將其重定向到其他頁面。

首先,為每個按鈕分配唯一的ID:

<input type="submit" id="adminLogin" value="Login as admin user">
<input type="submit" id="userLogin" value="Logins as Student">

現在,您可以通過jquery執行此操作:

<script>
  $(document).ready(function(){
     $("#adminLogin").click(function(){
          $('form').attr("action", "adminLogin.php");
     });
     $("#userLogin").click(function(){
          $('form').attr("action", "userLogin.php");
     });
  });
</script>

只需在數據庫表中添加一列,並指定用戶的角色是admin還是user,然后根據其值確定您相關的登錄頁面。

您可以users具有以下模式的表users

user_id |  name    | password  | type
1          admin     admin        1
2          memeber   member       0

例如,您可以將1作為管理員,將0作為成員。

因此,在形式上您可以擁有一個代碼,

<form action="validate.php" method="post">
Login: <input type="text" name="name">
Password: <input type="password" name="password">
<input type="submit" name="login" value="Login">
</form>

validate.php ,檢查他是管理員還是學生,

  <?php

  $db = mysqli_connect("specify host here","specify db username","specify db password","specify db name");

   if(isset($_POST['login']) // check whether user submits the form
   {

        $name_b = $_POST['name'];
        $password_b = $_POST['password'];

        $name = mysqli_real_escape_string($db,$name_b); // escaping special characters
        $password = mysqli_real_escape_string($db,$password_b);

        $query = "SELECT `type` FROM `users` WHERE `name` = '$name' AND `password` = `$password` LIMIT 1 ";
        $result = mysqli_query($db, $query)

        if($result)
        {
          $row = mysqli_fetch_assoc($result);

        $user_type = $row['type']; // you get user type here whether he's admin or login

        if ($user_type == 1) { 

             // this use is admin
            // do stuff here or redirect to admin panel or wherever you want
        }

        elseif ($user_type == 0) {
            // this user is member
            // do stuff here or redirect wherever you want
        }

        else
        {
            // if there's some other value, simplyredirect to login page again
        }
      } // close of if($result)
     else
     {
        echo "query failed"; // redirect to login page again
      }

    }

    else
    {
        // redirect to login page again         
    }




 ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM