Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - Sessions and Cookies
Identify the error in this PHP code snippet:
<?php
setcookie('user', 'John');
session_start();
$_SESSION['user'] = 'John';
?>
Asetcookie() must be called before any output
Bsession_start() must be called before setcookie()
CNo error, code is correct
Dsession_start() must be called before any output
Step-by-Step Solution
Solution:
  1. Step 1: Recall output rules for setcookie()

    setcookie() sends HTTP headers and must be called before any output.
  2. Step 2: Check code for output before setcookie()

    Code snippet does not show output, so no error here.
  3. Final Answer:

    No error, code is correct -> Option C
  4. Quick Check:

    Headers before output = setcookie() rule [OK]
Quick Trick: Call setcookie() before any HTML or echo output [OK]
Common Mistakes:
  • Thinking sessions store data on the client side
  • Assuming sessions do not require session_start()
  • Believing sessions automatically encrypt data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes