Authentication and Authorization in PHP Quiz Quiz

Challenge your understanding of PHP authentication and authorization concepts, including password handling, session management, user roles, and best security practices. This quiz is designed for beginners seeking to strengthen their knowledge in building secure PHP applications.

  1. Recognizing Secure Password Storage

    Which function is recommended for securely hashing passwords in PHP for user authentication?

    1. md5()
    2. crypt()
    3. password_hash()
    4. base64_encode()

    Explanation: The password_hash() function is designed specifically for hashing passwords securely, using strong algorithms and built-in salting. The md5() function is outdated and insecure, as it produces weak hashes vulnerable to attacks. base64_encode() is not a hashing function and can easily be reversed. Although crypt() can hash passwords, it requires manual intervention to select suitable algorithms and is less recommended compared to password_hash().

  2. Session-Based Authentication

    When a user logs in successfully, which PHP superglobal is typically used to store their authentication status?

    1. $_SESSION
    2. $_COOKIE
    3. $_FILES
    4. $_GET

    Explanation: $_SESSION is a superglobal used to store session data, making it ideal for maintaining a user's authentication status across pages. $_GET is used for HTTP GET variables and is not secure for authentication. $_COOKIE can store values but is sent to the client, making it less secure. $_FILES handles file uploads and is unrelated to user authentication.

  3. Limiting User Access by Role

    If you want only users with the role 'admin' to access a specific page, which approach is most appropriate in PHP?

    1. Rely solely on password complexity
    2. Check the user's role in the session before displaying content
    3. Hide the page link from non-admin users only in the HTML
    4. Store allowed roles in $_GET variables

    Explanation: Verifying the user's role stored in the session on the server side ensures only authorized users can access sensitive pages. Hiding links in HTML offers no real protection since users can still access the URL directly. Storing roles in $_GET variables is insecure as the data can easily be manipulated. Password complexity relates to authentication, not authorization.

  4. Understanding Authorization

    What is the main difference between authentication and authorization in a PHP application?

    1. Authentication verifies identity; authorization controls access levels
    2. Authentication happens after authorization
    3. Authentication stores data in databases; authorization does not
    4. Authentication uses cookies; authorization uses sessions

    Explanation: Authentication is about confirming who the user is, while authorization determines what the user is allowed to do. Both can use cookies or sessions depending on implementation, so option two is incorrect. Databases may be used for both, making option three inaccurate. Typically, authentication comes before authorization, so option four is also incorrect.

  5. Preventing Session Hijacking

    Which practice can help prevent session hijacking after user authentication in PHP?

    1. Using GET requests for sending login data
    2. Regenerating the session ID after login
    3. Enabling file uploads for all users
    4. Storing passwords in plain text

    Explanation: Regenerating the session ID immediately after login makes it harder for attackers to reuse an old session ID, improving security. Sending login data via GET requests is insecure as sensitive information appears in URLs. Storing passwords in plain text is unsafe, increasing the risk of data leaks. Allowing all users to upload files is unrelated and can create security vulnerabilities.

  6. User Logout Mechanics

    Which PHP function is commonly used to clear all session data for a user logging out?

    1. setcookie()
    2. session_destroy()
    3. header()
    4. mysqli_close()

    Explanation: session_destroy() removes all session data, effectively logging the user out. setcookie() manages cookies but does not clear session data by itself. header() is used for changing HTTP headers and does not handle session destruction. mysqli_close() is related to database connections, not session management.

  7. Token-Based Authorization

    In a PHP REST API, which feature is commonly used to ensure that only authenticated clients can access protected endpoints?

    1. HTML form submissions
    2. JavaScript alerts
    3. CSS classes
    4. Access tokens in HTTP headers

    Explanation: Access tokens sent in HTTP headers allow APIs to verify requests are made by authenticated users, ensuring secure access to protected resources. HTML forms are not suited for API authentication as they do not provide proof of identity for each request. JavaScript alerts or CSS classes play no role in securing API endpoints.

  8. Handling Failed Logins

    After several failed login attempts, what is a recommended action to enhance security in a PHP authentication system?

    1. Redirect to a random webpage
    2. Temporarily lock the user account or delay responses
    3. Delete all user records from the database
    4. Display full error messages including user data

    Explanation: Locking the account or delaying responses prevents rapid brute-force attempts and protects against automated attacks. Showing detailed error messages with user data is risky, as it could disclose sensitive information. Random redirections confuse users without adding security. Deleting all user records is excessive and not a realistic solution to failed logins.

  9. Cross-Site Scripting (XSS) and Authentication

    Why is it important to use output escaping when displaying usernames on authenticated pages in PHP?

    1. To allow users to add special characters to usernames
    2. To bypass password checks
    3. To speed up PHP code execution
    4. To prevent cross-site scripting attacks

    Explanation: Escaping output helps prevent XSS, where attackers might inject malicious scripts via input fields such as usernames. Output escaping has no impact on performance (so it doesn't speed up code), nor does it help bypass password checks. While escaping may allow certain characters, its main purpose is preventing attacks, not enabling unusual usernames.

  10. Keeping User Credentials Safe

    Which is the safest way to compare a user's submitted password with a stored hash in PHP?

    1. Using strcmp() directly on the hash
    2. Using password_verify() function
    3. Checking if lengths of strings match
    4. Comparing with double equals (==)

    Explanation: password_verify() is designed to securely check if a password matches a stored hash, handling nuances of different hashing algorithms. Using strcmp() or double equals does not account for hashing techniques and may introduce timing attacks. Simply comparing string lengths is insufficient and can produce false positives.