Bird
0
0

What is wrong with this code to destroy a session?

medium📝 Debug Q7 of 15
PHP - Sessions and Cookies
What is wrong with this code to destroy a session?
session_start();
$_SESSION = array();
session_destroy();
setcookie(session_name(), '', time() + 3600);
AThe cookie expiration time should be in the past to delete it
B$_SESSION should not be emptied before session_destroy()
Csession_destroy() must be called before session_start()
Dsession_name() cannot be used with setcookie()
Step-by-Step Solution
Solution:
  1. Step 1: Understand cookie deletion

    To delete a cookie, its expiration time must be set to a past time, not a future time.
  2. Step 2: Analyze setcookie() call

    The code sets the cookie expiration to time() + 3600, which extends the cookie life by 1 hour instead of deleting it.
  3. Final Answer:

    The cookie expiration time should be in the past to delete it -> Option A
  4. Quick Check:

    Delete cookie by setting expiration in the past [OK]
Quick Trick: Set cookie expiration to past time to delete it [OK]
Common Mistakes:
  • Setting cookie expiration to future time when deleting
  • Confusing session_destroy() order
  • Misusing session_name() function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes