Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php和mysql将用户带到基于其角色的特定页面_Php_Mysql - Fatal编程技术网

使用php和mysql将用户带到基于其角色的特定页面

使用php和mysql将用户带到基于其角色的特定页面,php,mysql,Php,Mysql,我在MySQL数据库中有一个表,存储用户及其角色,比如管理员和学生。 所以我想当管理员登录时,会被带到管理员仪表板,学生会被带到学生主页 <?php if (isset($conn,$_POST['login'])) { $username = mysqli_real_escape_string($conn, $_POST["username"]); $password = mysqli_real_escape

我在MySQL数据库中有一个表,存储用户及其角色,比如管理员和学生。 所以我想当管理员登录时,会被带到管理员仪表板,学生会被带到学生主页

<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>
如何根据两个用户的角色将其重定向到各自的页面

<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>

提前感谢

给他们指定一个管理员角色,例如学生,登录后,做类似的事情

<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>
$role=从数据库中获取角色

<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>
ifrole=='admin'{然后重定向管理面板}

<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>
elseifrole=='student'{重定向到student place}

<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>

否则{重定向到另一个地方}

1。首先在数据库表字段中添加用户类型,您的数据库表结构如下:-

table:-
    id    username   password   Role 
    1      ---         ----     admin
    2       ---        ----     Student
    3       ----        ----    Teacher
<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>
我认为你的问题是检查条件,使用if和else语句

 $_SESSION['username'] = $name[0];
  $_SESSION['password'] = $name[1];
  $_SESSION['Role'] = $name[2];


if($_SESSION['Role'] == 'admin'){
   header("Location: admin.php");
   }
else if($_SESSION['Role'] == 'Student'){
   header("Location: Student.php");
   }
else if($_SESSION['Role'] == 'Teacher'){
   header("Location: Teacher.php");
   }
else{
   echo "Your not logged in";
   }
<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>
希望它能帮助你……

这对我很有用。谢谢你的帮助

<?php if (isset($conn,$_POST['login'])) {
                   $username = mysqli_real_escape_string($conn, $_POST["username"]);  
                   $password = mysqli_real_escape_string($conn, $_POST["password"]);  
                   $sql = "SELECT * FROM staff WHERE Domain = '$username'";  
                   $result = mysqli_query($conn, $sql);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row["Pass"]))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;
                          $role=$row['Role'];

                          if($role == 'Admin'){ 
                           header("location: dashboard.php");
                          }

                          elseif($role=='Staff'){
                           header("location: index.php");

                          }

                          else{
                           header("location: login.php");
                          } 
                     }  
                     else {  
                          //return false;  
                          echo '<script>alert("Wrong User Password")</script>'; 
                          }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Sorry! No such User Name is found")</script>';  
           }    

                    } 
                ?>

首先,您尝试并向我们展示代码您做了什么?到目前为止您尝试了什么??所以在登录后,他们想要重定向到他们的特定页面,否则他们将无法看到其他页面。请解释。我将附上我尝试过的内容,但现在我有两个导航栏,一个用于管理员,另一个用于员工。如果用户不属于该角色,我已成功将其重定向到登录。但现在的挑战是使用会话在登录页面重定向它们。对不起,我是新来的,所以我不知道如何附加我的代码。但是,它似乎采取了第一个if,而忽略了else语句。它终于对你的答案起作用了。非常感谢拉沙·帕雷希什维利。