mercredi 2 juin 2021

How to give login permission for multi user?

What i did is I have created two users admin and agent ? If the user admin login through his Id he should redirected to admin.php same as while user agent login he should redirected to entry.php ? The problem is whenever i log using admin id it goes to entry.php I have attached the index page of the code

<?php  
 $connect = mysqli_connect("localhost", "root", "", "checks");  
 session_start();  
 if(isset($_SESSION["username"]))  
 {  
      header("location:entry.php");  
 }  
 if(isset($_POST["register"]))  
 {  
      if(empty($_POST["username"]) && empty($_POST["password"]))  
      {  
           echo '<script>alert("Both Fields are required")</script>';  
      }  
      else  
      {  
          $name = mysqli_real_escape_string($connect, $_POST["name"]); 
           $username = mysqli_real_escape_string($connect, $_POST["username"]);  
           $Emp_id = mysqli_real_escape_string($connect, $_POST["Emp_id"]); 
           $password = mysqli_real_escape_string($connect, $_POST["password"]);  
           $password = md5($password);  
           $role=mysqli_real_escape_string($connect, $_POST["role"]); 
           $query = "INSERT INTO users (name,username,Emp_id, password,role) VALUES('$name','$username',' $Emp_id', '$password','$role')";  
           if(mysqli_query($connect, $query))  
           {  
                echo '<script>alert("Registration Done")</script>';  
           }  
      }  
 }  
 if(isset($_POST["login"]))  
 {  
      if(empty($_POST["username"]) && empty($_POST["password"]) )  
      {  
           echo '<script>alert("ALL Fields are required")</script>';  
      }  
      else  
      {  
           $username = mysqli_real_escape_string($connect, $_POST["username"]);  
           $password = mysqli_real_escape_string($connect, $_POST["password"]);  
           $password = md5($password); 
          $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";  
           $result = mysqli_query($connect, $query);
           $row=mysqli_fetch_array($result);
      echo($query);
      echo($row);
           if($row["role"]=="agent")  
           {  
                $_SESSION['username'] = $username;  
                header("location:entry.php");  
           }  
           elseif($row["role"]=="admin") 
            {  $_SESSION['username'] = $username;  
                header("location:admin.php"); 

           }
           else
           {  
                echo '<script>alert("Wrong User Details")</script>';  
           }  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>WELCOME!!!</title>  
           
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:500px;">  
                
                <br />  
                <?php  
                if(isset($_GET["action"]) == "login")  
                {  
                ?>  
                <h3 align="center">Login</h3>  
                <br />  
                <form method="post">  
                     <label>Enter Username</label>  
                     <input type="text" name="username" class="form-control" />  
                     <br />  
                     <label>Enter Password</label>  
                     <input type="password" name="password" class="form-control" />  
                     <br />  
           <input type="submit" name="login" value="Login" class="btn btn-info" />  
                     <br />  
                     <p align="center"><a href="index1.php">Register</a></p>  
                </form>  
                <?php       
                }  
                else  
                {  
                ?>  
                <h3 align="center">Register</h3>  
                <br />  
                <form method="post">  
                    <label>Enter Name</label>  
                     <input type="text" name="name" class="form-control" />
                     <br>
                     <label>Enter Username</label>  
                     <input type="text" name="username" class="form-control" />  
                     <br />  
                     <label>Enter Your Employee Id</label>  
                     <input type="text" name="Emp_id" class="form-control" />
                     <br>
                     <label>Enter Password</label>  
                     <input type="password" name="password" class="form-control" />  
                     <br />  
                                 <br />  
          <select type="dropdown" name = "role">
            <option value = "admin" selected>Admin</option>
            <option value = "agent">Agent</option>
            <option value = "superior">Superior</option>
         </select>
                     <input type="submit" name="register" value="Register" class="btn btn-info" />  
                     <br />  
                     <p align="center"><a href="index1.php?action=login">Login</a></p>  
                </form>  
                <?php  
                }  
                ?>  
           </div>  
      </body>  
 </html>  

Aucun commentaire:

Enregistrer un commentaire