Advertisement
AzisMM

login.php

Oct 30th, 2023
2,273
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <?php
  2.  
  3. // Database connection
  4. $conn = mysqli_connect("localhost", "root", "", "example_db");
  5.  
  6. // Login logic
  7. if(isset($_POST['login'])) {
  8.     $username = $_POST['username'];
  9.     $password = $_POST['password'];
  10.    
  11.     // Check if user is admin
  12.     $query = "SELECT * FROM users WHERE username='$username' AND password='$password' AND role='admin'";
  13.     $result = mysqli_query($conn, $query);
  14.     $isAdmin = mysqli_num_rows($result) > 0;
  15.  
  16.     // Check if user is student
  17.     $query = "SELECT * FROM users WHERE username='$username' AND password='$password' AND role='student'";
  18.     $result = mysqli_query($conn, $query);
  19.     $isStudent = mysqli_num_rows($result) > 0;
  20.  
  21.     if($isStudent || $isAdmin) {
  22.         // Start session and store role
  23.         session_start();
  24.         $_SESSION['role'] = $isAdmin ? 'admin' : 'student';
  25.  
  26.         // Redirect to appropriate page
  27.         if($isAdmin) {
  28.             header("Location: admin_dashboard.php");
  29.         } else {
  30.             header("Location: student_dashboard.php");
  31.         }
  32.     } else {
  33.         echo "Invalid login credentials";
  34.     }
  35. }
  36.  
  37. ?>
  38.  
  39. <!DOCTYPE html>
  40. <html>
  41. <head>
  42.     <title>Login and Register Form</title>
  43.     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  44.  
  45. </head>
  46. <body>
  47.     <h1>Login</h1>
  48.     <form method="POST">
  49.         <input type="text" name="username" placeholder="Username" required><br>
  50.         <input type="password" name="password" placeholder="Password" required><br>
  51.         <button type="submit" name="login">Login</button>
  52.     </form>
  53. </body>
  54. </html>
Advertisement
Comments
  • hoss
    215 days
    # text 0.08 KB | 0 0
    1. Please add password hashing. You should not be storing passwords as plain text.
Add Comment
Please, Sign In to add comment
Advertisement