What potential pitfalls should be considered when implementing admin privileges in PHP?

When implementing admin privileges in PHP, it is essential to consider potential security pitfalls such as SQL injection, cross-site scripting (XSS), and insufficient input validation. To mitigate these risks, always sanitize user inputs, use prepared statements for database queries, and implement proper authentication and authorization mechanisms.

// Sanitize user input to prevent SQL injection
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);

// Use prepared statements to prevent SQL injection
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();

// Implement proper authentication and authorization mechanisms
if ($result->num_rows > 0) {
    $user = $result->fetch_assoc();
    if ($user['role'] === 'admin') {
        // User is an admin, grant privileges
    } else {
        // User is not authorized to access admin privileges
    }
} else {
    // Invalid credentials
}