📖 Scenario: You are building a simple website where users can log in. To remember who is logged in, you will use PHP sessions. Sessions help keep information about the user while they browse different pages.
🎯 Goal: Create a PHP script that starts a session, stores a username in the session, and then reads and displays that username on the page.
📋 What You'll Learn
Start a session using session_start()
Store a username in the $_SESSION superglobal array
Retrieve and display the username from the session
💡 Why This Matters
🌍 Real World
Sessions are used on websites to remember who a user is after they log in, so they don't have to enter their information on every page.
💼 Career
Understanding sessions is important for web developers to manage user login states and personalize user experiences.
Progress0 / 4 steps
1
Start a session
Write session_start(); at the top of your PHP script to begin a session.
PHP
Hint
Sessions must be started before any HTML or output is sent.
2
Store a username in the session
After session_start();, add the line $_SESSION['username'] = 'Alice'; to save the username 'Alice' in the session.
PHP
Hint
Use the $_SESSION array to save data that you want to keep across pages.
3
Retrieve the username from the session
Add a line to get the username from the session using $user = $_SESSION['username'];.
PHP
Hint
Assign the session value to a variable to use it easily later.
4
Display the username
Write echo "Logged in user: $user"; to show the username stored in the session.
PHP
Hint
Use echo to print the username on the page.
Practice
(1/5)
1. What is the main purpose of using session_start() in PHP?
easy
A. To begin a session and enable storing data across pages
B. To end a session and clear all session data
C. To connect to a database for user authentication
D. To send cookies to the user's browser
Solution
Step 1: Understand what session_start() does
This function initializes a session or resumes the current one based on a session identifier passed via a cookie or URL.
Step 2: Recognize session purpose
Sessions allow data to be saved and accessed across multiple pages for the same user, such as login status or preferences.
Final Answer:
To begin a session and enable storing data across pages -> Option A
Quick Check:
session_start() starts session = D [OK]
Hint: Remember: session_start() must run before any output [OK]
Common Mistakes:
Thinking session_start() ends a session
Confusing sessions with database connections
Calling session_start() after HTML output
2. Which of the following is the correct way to start a session in PHP?
easy
A. session.begin();
B. start_session();
C. session_start();
D. begin_session();
Solution
Step 1: Recall PHP session function syntax
The correct function to start a session is session_start() with an underscore and parentheses.
Step 2: Check other options for syntax errors
Options B, C, and D are not valid PHP functions and will cause errors.
Final Answer:
session_start(); -> Option C
Quick Check:
Correct PHP session start syntax = A [OK]
Hint: Use exact function name: session_start() [OK]
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.