Bird
0
0

Identify the error in this PHP code that tries to read a cookie named session:

medium📝 Debug Q14 of 15
PHP - Sessions and Cookies
Identify the error in this PHP code that tries to read a cookie named session:
<?php
echo $_COOKIE['session'];
?>
ACookie name should be uppercase
BWrong function used to read cookie
CMissing check if cookie exists before reading
DCookies cannot be read in PHP
Step-by-Step Solution
Solution:
  1. Step 1: Understand cookie reading best practice

    Directly accessing $_COOKIE['session'] without checking if it exists can cause an undefined index notice if the cookie is not set.
  2. Step 2: Identify the missing check

    The code should use isset($_COOKIE['session']) before echoing to avoid errors.
  3. Final Answer:

    Missing check if cookie exists before reading -> Option C
  4. Quick Check:

    Always check isset() before reading cookie [OK]
Quick Trick: Always check isset() before using $_COOKIE values [OK]
Common Mistakes:
  • Ignoring isset() check causing warnings
  • Thinking cookie names are case sensitive in $_COOKIE
  • Believing cookies cannot be read in PHP

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes