Working with PHP Sessions

Tutorial 1 of 5

1. Introduction

1.1 Tutorial's Goal

This tutorial aims to provide a comprehensive understanding of how to work with PHP sessions. By the end of this tutorial, you will be able to create, manage, and destroy PHP sessions effectively.

1.2 Learning Outcomes

  • Understanding what PHP sessions are and how they work
  • Starting a PHP session
  • Storing and retrieving session data
  • Destroying a PHP session

1.3 Prerequisites

To follow along with this tutorial, you should have a basic understanding of PHP and how to run PHP scripts on a server.

2. Step-by-Step Guide

2.1 PHP Sessions

A PHP session is used to store information on the server for later use. Session data is stored on the server, not the client. This makes sessions a secure way of storing user information compared to cookies.

2.2 Starting a PHP Session

To start a PHP session, you use the session_start() function. This function must be the very first thing in your document, before any HTML tags.

<?php
// Starting a session
session_start();
?>

2.3 Storing Session Data

You can store data in a PHP session by setting the $_SESSION superglobal variable. This variable is an associative array holding session variables.

<?php
// Start the session
session_start();

// Set session variables
$_SESSION["username"] = "JohnDoe";
$_SESSION["email"] = "johndoe@example.com";
?>

2.4 Retrieving Session Data

To retrieve the session data, you again use the $_SESSION superglobal. Remember to call session_start() before trying to access the session variables.

<?php
// Start the session
session_start();

// Echo session variables
echo "Username is: " . $_SESSION["username"] . ".<br>";
echo "Email is: " . $_SESSION["email"] . ".";
?>

2.5 Destroying a PHP Session

You can destroy a PHP session by using the session_destroy() function. This function does not need any argument and will destroy all the session data.

<?php
// Start the session
session_start();

// Destroy the session
session_destroy();
?>

3. Code Examples

3.1 Starting a Session

<?php
// Starting a session
session_start();
?>

In the above code, session_start() is a PHP function that starts a new session or resumes an existing one.

3.2 Storing and Retrieving Session Data

<?php
// Start the session
session_start();

// Set session variables
$_SESSION["username"] = "JohnDoe";
$_SESSION["email"] = "johndoe@example.com";

// Echo session variables
echo "Username is: " . $_SESSION["username"] . ".<br>";
echo "Email is: " . $_SESSION["email"] . ".";
?>

In this example, we first start the session using session_start(). Then, we store data in the session using the $_SESSION superglobal array. We assign the values "JohnDoe" and "johndoe@example.com" to the keys "username" and "email", respectively. We then retrieve and print these values using the echo statement.

3.3 Destroying a Session

<?php
// Start the session
session_start();

// Destroy the session
session_destroy();
?>

In the above example, we start the session with session_start() and then destroy it using session_destroy().

4. Summary

In this tutorial, we have covered the basics of working with PHP sessions. We have learned how to start a session, store and retrieve data from a session, and finally, how to destroy a session.

For further learning, you might find it useful to look at how to handle sessions in a real-world PHP application, including handling user login and logout.

5. Practice Exercises

Exercise 1

Write a PHP script that starts a session, sets a session variable named 'viewCount' and increments it by 1 each time the page is refreshed.

Exercise 2

Write a PHP script that stores an array of your favorite movies in a session variable and display them on the page.

Exercise 3

Write a PHP script that starts a session, sets some session variables, and then destroys the session. Verify that the session data has been destroyed.

Remember, practice is key to mastering any programming language. Happy coding!