Bird
0
0

You want to completely log out a user by destroying the session and removing the session cookie. Which of the following code snippets correctly achieves this?

hard📝 Application Q15 of 15
PHP - Sessions and Cookies
You want to completely log out a user by destroying the session and removing the session cookie. Which of the following code snippets correctly achieves this?
Asession_start(); session_unset(); session_destroy(); setcookie(session_name(), '', time() - 3600);
Bsession_destroy(); session_unset(); setcookie(session_name(), '', time() + 3600);
Csession_start(); session_destroy(); setcookie(session_name(), '', time() + 3600);
Dsession_unset(); session_start(); setcookie(session_name(), '', time() - 3600);
Step-by-Step Solution
Solution:
  1. Step 1: Start the session to access session data

    Call session_start() to initialize the session before modifying it.
  2. Step 2: Clear session variables and destroy session

    Use session_unset() to clear variables, then session_destroy() to end the session.
  3. Step 3: Remove the session cookie from the client

    Call setcookie() with the session name and a past expiration time to delete the cookie.
  4. Final Answer:

    session_start(); session_unset(); session_destroy(); setcookie(session_name(), '', time() - 3600); -> Option A
  5. Quick Check:

    Start, unset, destroy, delete cookie = A [OK]
Quick Trick: Start session, unset, destroy, then expire cookie [OK]
Common Mistakes:
  • Not starting session before destroying
  • Setting cookie expiration in the future instead of past
  • Not removing session cookie after destroying session

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes