The code uses $_SESSION but never calls session_start() to begin the session.
Step 2: Understand session usage rules
Without calling session_start(), PHP does not know which session to use, causing errors or empty values.
Final Answer:
Missing session_start() before using $_SESSION -> Option D
Quick Check:
Always call session_start() before $_SESSION [OK]
Hint: Always start session before accessing $_SESSION [OK]
Common Mistakes:
Using $_SESSION without session_start()
Assuming $_SESSION works like normal variables
Trying to echo $_SESSION without initialization
5. You want to count how many times a user visits a page using sessions. Which code correctly increments the visit count each time the page loads?
<?php
// Fill in the missing part
if (isset($_SESSION['visits'])) {
$_SESSION['visits']++;
} else {
$_SESSION['visits'] = 1;
}
echo "Visit number: " . $_SESSION['visits'];
?>
hard
A. session_start();
B. session_destroy();
C. session_unset();
D. session_write_close();
Solution
Step 1: Recognize session must be started
To use $_SESSION variables, the session must be started with session_start(); at the top.
Step 2: Understand the code logic
The code checks if 'visits' exists in the session, increments it if yes, or sets it to 1 if no, then prints the count.
Final Answer:
session_start(); -> Option A
Quick Check:
Start session before using $_SESSION to track visits [OK]
Hint: Always call session_start() before session variables [OK]
Common Mistakes:
Forgetting session_start() causes no session data
Using session_destroy() resets visits
Confusing session_unset() with starting session
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.