Bird
0
0

Identify the error in this PHP code snippet for setting a secure cookie:

medium📝 Debug Q14 of 15
PHP - Sessions and Cookies
Identify the error in this PHP code snippet for setting a secure cookie:
setcookie('session', 'abc123', time() + 3600, '/', '', 'true', true);
AThe secure flag is a string instead of boolean
BThe expiration time is in the past
CThe cookie name is invalid
DMissing path parameter
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter types in setcookie()

    The secure parameter must be a boolean (true/false), not a string.
  2. Step 2: Identify the incorrect usage

    Here, 'true' is a string, which PHP treats as true but is incorrect usage and can cause unexpected behavior.
  3. Final Answer:

    The secure flag is a string instead of boolean -> Option A
  4. Quick Check:

    Secure flag must be boolean, not string [OK]
Quick Trick: Use true/false without quotes for flags [OK]
Common Mistakes:
  • Using 'true' or 'false' as strings
  • Forgetting to set secure flag for HTTPS
  • Omitting required parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes