Bird
0
0

You want to set a cookie that is only available on the /admin path of your website. Which setcookie() call is correct?

hard📝 Application Q8 of 15
PHP - Sessions and Cookies
You want to set a cookie that is only available on the /admin path of your website. Which setcookie() call is correct?
Asetcookie('role', 'admin', time() + 3600, '/');
Bsetcookie('role', 'admin', time() + 3600, 'admin');
Csetcookie('role', 'admin', time() + 3600);
Dsetcookie('role', 'admin', time() + 3600, '/admin');
Step-by-Step Solution
Solution:
  1. Step 1: Understand the path parameter in setcookie()

    The fourth parameter defines the path on the server where the cookie is available. It must start with a slash.
  2. Step 2: Evaluate each option

    setcookie('role', 'admin', time() + 3600, '/admin'); correctly sets path to '/admin'. setcookie('role', 'admin', time() + 3600, 'admin'); misses the leading slash. setcookie('role', 'admin', time() + 3600); uses default path '/'. setcookie('role', 'admin', time() + 3600, '/'); sets path to root, not '/admin'.
  3. Final Answer:

    setcookie('role', 'admin', time() + 3600, '/admin'); -> Option D
  4. Quick Check:

    Cookie path starts with '/' [OK]
Quick Trick: Cookie path must start with '/' to restrict location [OK]
Common Mistakes:
  • Omitting leading slash in path
  • Using default path when specific path needed
  • Confusing path with domain parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes