Bird
0
0

Which of the following is the correct way to set a cookie named session_id with value xyz123 that expires after 30 minutes in PHP?

easy📝 Syntax Q3 of 15
PHP - Sessions and Cookies
Which of the following is the correct way to set a cookie named session_id with value xyz123 that expires after 30 minutes in PHP?
Asetcookie('session_id', 'xyz123', time() + 3600);
Bsetcookie('session_id', 'xyz123', time() + 1800);
Csetcookie('session_id', 'xyz123', time() - 1800);
Dsetcookie('session_id', 'xyz123');
Step-by-Step Solution
Solution:
  1. Step 1: Understand the expiration time

    30 minutes equals 1800 seconds.
  2. Step 2: Use time() + 1800 as the expiration timestamp

    The third parameter of setcookie() is the expiration time in Unix timestamp format.
  3. Final Answer:

    setcookie('session_id', 'xyz123', time() + 1800); -> Option B
  4. Quick Check:

    Expiration time is 30 minutes (1800 seconds) [OK]
Quick Trick: Use time() + seconds for expiration [OK]
Common Mistakes:
  • Using time() + 3600 for 30 minutes instead of 1 hour
  • Setting expiration time in the past to create cookie
  • Omitting expiration time for persistent cookie

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes