0
0
PHPprogramming~15 mins

Destroying sessions in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Destroying sessions
📖 Scenario: You are building a simple PHP web application that uses sessions to keep track of user login status. When the user logs out, you want to safely destroy the session to protect their information.
🎯 Goal: Learn how to start a session, set session variables, and then properly destroy the session when the user logs out.
📋 What You'll Learn
Create a session and set a session variable
Add a variable to track if the user is logged in
Write code to destroy the session safely
Print a message confirming the session is destroyed
💡 Why This Matters
🌍 Real World
Websites use sessions to remember who you are when you log in. Destroying sessions properly helps keep your account safe when you log out.
💼 Career
Web developers need to manage sessions securely to protect user data and prevent unauthorized access.
Progress0 / 4 steps
1
Start a session and set a session variable
Write PHP code to start a session using session_start() and then set a session variable called username with the value 'Alice'.
PHP
Need a hint?

Use session_start() at the top to begin the session. Then assign the username to $_SESSION['username'].

2
Add a variable to track login status
Add a session variable called logged_in and set it to true to indicate the user is logged in.
PHP
Need a hint?

Set $_SESSION['logged_in'] = true; to mark the user as logged in.

3
Destroy the session safely
Write PHP code to destroy the session safely by calling session_unset() and session_destroy().
PHP
Need a hint?

Use session_unset() to clear all session variables, then session_destroy() to end the session.

4
Print confirmation message
Write a print statement to display the message 'Session destroyed successfully.'.
PHP
Need a hint?

Use print('Session destroyed successfully.'); to show the message.