0
0
PHPprogramming~15 mins

Starting and using sessions in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Starting and Using Sessions in PHP
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo to print the username on the page.