Bird
0
0

You want to set a cookie named theme with value dark that expires in 7 days. Which code correctly sets this cookie?

hard📝 Application Q15 of 15
PHP - Sessions and Cookies
You want to set a cookie named theme with value dark that expires in 7 days. Which code correctly sets this cookie?
Asetcookie('theme', 'dark', time() + 60);
Bsetcookie('theme', 'dark', time() + 7 * 24 * 60 * 60);
Csetcookie('theme', 'dark', 7);
Dsetcookie('theme', 'dark', time() - 7 * 24 * 60 * 60);
Step-by-Step Solution
Solution:
  1. Step 1: Calculate expiration time for 7 days

    7 days in seconds = 7 * 24 hours * 60 minutes * 60 seconds = 604800 seconds.
  2. Step 2: Use time() plus 7 days for expiration

    The expiration parameter in setcookie() should be current time plus 604800 seconds.
  3. Step 3: Check options for correct expiration

    setcookie('theme', 'dark', time() + 7 * 24 * 60 * 60); correctly uses time() + 7 * 24 * 60 * 60. setcookie('theme', 'dark', time() + 60); sets only 60 seconds, C uses 7 (invalid), D sets a past time (cookie expires immediately).
  4. Final Answer:

    setcookie('theme', 'dark', time() + 7 * 24 * 60 * 60); -> Option B
  5. Quick Check:

    Expire = now + 7 days in seconds [OK]
Quick Trick: Expire time = time() + days * 86400 seconds [OK]
Common Mistakes:
  • Using small numbers like 7 or 60 instead of seconds
  • Setting expiration in the past
  • Forgetting to add time() to expiration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes