Bird
0
0

Identify the error in the following code snippet that aims to destroy a session:

medium📝 Debug Q14 of 15
PHP - Sessions and Cookies
Identify the error in the following code snippet that aims to destroy a session:
session_unset();
session_destroy();
ANo error, code works fine
BMissing session_start() before unsetting and destroying
Csession_unset() does not clear session variables
Dsession_destroy() should be called before session_unset()
Step-by-Step Solution
Solution:
  1. Step 1: Check session lifecycle requirements

    Before accessing or modifying session data, session_start() must be called to initialize the session.
  2. Step 2: Analyze the given code

    The code calls session_unset() and session_destroy() without starting the session, which causes errors or unexpected behavior.
  3. Final Answer:

    Missing session_start() before unsetting and destroying -> Option B
  4. Quick Check:

    Always start session before unsetting/destroying [OK]
Quick Trick: Always call session_start() before session_unset() or session_destroy() [OK]
Common Mistakes:
  • Forgetting to call session_start() first
  • Calling session_destroy() before session_unset() (not an error but unusual)
  • Assuming session_unset() destroys session

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes