📖 Scenario: You are building a simple website that remembers a visitor's name during their visit. This helps the site greet them personally on each page.
🎯 Goal: Create a PHP script that starts a session, saves a visitor's name in the session, and then retrieves and displays that name on the page.
📋 What You'll Learn
Start a PHP session using session_start()
Create a session variable called user_name and assign it the value 'Alice'
Retrieve the user_name from the session
Display a greeting message using the session value
💡 Why This Matters
🌍 Real World
Sessions help websites remember who you are as you move between pages, like keeping your shopping cart or login info.
💼 Career
Understanding sessions is key for web developers to manage user data securely and create personalized experiences.
Progress0 / 4 steps
1
Start a PHP session
Write session_start(); at the beginning of your PHP script to start a session.
PHP
Hint
Use the session_start() function at the very top of your PHP file.
2
Save a name in the session
After starting the session, create a session variable called user_name and set it to 'Alice' using $_SESSION.
PHP
Hint
Use $_SESSION['user_name'] = 'Alice'; to store the name.
3
Retrieve the name from the session
Create a variable called name and assign it the value stored in the session variable user_name.
PHP
Hint
Use $name = $_SESSION['user_name']; to get the stored name.
4
Display a greeting using the session name
Write a echo statement to display: Hello, Alice! Welcome back. using the variable $name.
PHP
Hint
Use echo "Hello, $name! Welcome back."; to show the greeting.
Practice
(1/5)
1. What is the main purpose of using sessions in PHP?
easy
A. To remember user data across multiple pages
B. To store data permanently on the user's computer
C. To speed up the website loading time
D. To create a new database connection
Solution
Step 1: Understand what sessions do
Sessions keep track of user information as they move between pages on a website.
Step 2: Compare options
Only To remember user data across multiple pages correctly describes this purpose. Options A, C, and D describe unrelated functions.
Final Answer:
To remember user data across multiple pages -> Option A
Quick Check:
Sessions remember user data = B [OK]
Hint: Sessions remember data across pages, not store on user PC [OK]
Common Mistakes:
Confusing sessions with cookies stored on user device
Thinking sessions speed up page loading
Believing sessions create database connections
2. Which PHP function must be called before using $_SESSION variables?
easy
A. session_end()
B. session_open()
C. session_init()
D. session_start()
Solution
Step 1: Identify the correct function to start a session
The function session_start() initializes a session or resumes the current one.
Step 2: Check other options
Functions like session_end(), session_open(), and session_init() do not exist in PHP.
Final Answer:
session_start() -> Option D
Quick Check:
Start session with session_start() = C [OK]
Hint: Always call session_start() before accessing $_SESSION [OK]
Common Mistakes:
Using non-existent functions like session_end()
Forgetting to call session_start() before $_SESSION