Bird
0
0

Which of the following is the correct way to set a cookie with secure and HttpOnly flags in PHP?

easy📝 Syntax Q12 of 15
PHP - Sessions and Cookies
Which of the following is the correct way to set a cookie with secure and HttpOnly flags in PHP?
Asetcookie('user', 'John', time() + 3600, '/', '', true, false);
Bsetcookie('user', 'John', time() + 3600, '/', '', false, true);
Csetcookie('user', 'John', time() + 3600, '/', '', true, true);
Dsetcookie('user', 'John', time() + 3600, '/', true, true);
Step-by-Step Solution
Solution:
  1. Step 1: Recall setcookie() parameters order

    The parameters are: name, value, expire, path, domain, secure, httponly.
  2. Step 2: Identify correct secure and httponly flags

    Setting secure to true means cookie sent only over HTTPS, and httponly true means inaccessible to JavaScript.
  3. Final Answer:

    setcookie('user', 'John', time() + 3600, '/', '', true, true); -> Option C
  4. Quick Check:

    Secure and HttpOnly flags = true, true [OK]
Quick Trick: Secure is 6th, HttpOnly is 7th parameter in setcookie [OK]
Common Mistakes:
  • Mixing order of parameters
  • Setting secure or httponly to false by mistake
  • Omitting parameters causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes