What are some best practices for implementing a login system with different user types in PHP?

When implementing a login system with different user types in PHP, it is important to have a clear distinction between the different user types (e.g., admin, regular user) and to securely authenticate and authorize users based on their type. One approach is to have a user_type field in the database that indicates the type of user, and to check this field during the login process to determine the user's access level.

// Assuming you have a users table with a user_type field

// Check if the user exists and verify their password
$query = "SELECT * FROM users WHERE username = :username";
$stmt = $pdo->prepare($query);
$stmt->execute(['username' => $username]);
$user = $stmt->fetch();

if ($user && password_verify($password, $user['password'])) {
    // Check the user type and redirect accordingly
    if ($user['user_type'] == 'admin') {
        header("Location: admin_dashboard.php");
    } else {
        header("Location: user_dashboard.php");
    }
} else {
    echo "Invalid username or password";
}