Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Database connection
- $conn = mysqli_connect("localhost", "root", "", "example_db");
- // Login logic
- if(isset($_POST['login'])) {
- $username = $_POST['username'];
- $password = $_POST['password'];
- // Check if user is admin
- $query = "SELECT * FROM users WHERE username='$username' AND password='$password' AND role='admin'";
- $result = mysqli_query($conn, $query);
- $isAdmin = mysqli_num_rows($result) > 0;
- // Check if user is student
- $query = "SELECT * FROM users WHERE username='$username' AND password='$password' AND role='student'";
- $result = mysqli_query($conn, $query);
- $isStudent = mysqli_num_rows($result) > 0;
- if($isStudent || $isAdmin) {
- // Start session and store role
- session_start();
- $_SESSION['role'] = $isAdmin ? 'admin' : 'student';
- // Redirect to appropriate page
- if($isAdmin) {
- header("Location: admin_dashboard.php");
- } else {
- header("Location: student_dashboard.php");
- }
- } else {
- echo "Invalid login credentials";
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Login and Register Form</title>
- <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
- </head>
- <body>
- <h1>Login</h1>
- <form method="POST">
- <input type="text" name="username" placeholder="Username" required><br>
- <input type="password" name="password" placeholder="Password" required><br>
- <button type="submit" name="login">Login</button>
- </form>
- </body>
- </html>
Advertisement
Comments
-
- Please add password hashing. You should not be storing passwords as plain text.
Add Comment
Please, Sign In to add comment
Advertisement