Understanding and Implementing Sessions in PHP

Andrei Birta
3 min readJan 16, 2023

--

In PHP, a session is a way to store information on the server side for a specific user. This information can be accessed and modified throughout the user’s interaction with the website, allowing for a more personalized and consistent experience for the user.

To start a new session in PHP, the session_start() function must be called at the beginning of the script. This function will either create a new session or resume an existing one, and will also generate a unique session ID that can be used to identify the user throughout the session.

Once a session has been started, data can be stored in the session using the $_SESSION superglobal array. This array can be accessed and modified just like any other PHP array, with the added benefit that the data stored in it will persist across multiple pages and requests.
For example, the following code will store a value in the session:

<?php
session_start();
$_SESSION["username"] = "JohnDoe";
?>

And this code will retrieve the value:

<?php
session_start();
echo $_SESSION["username"]; // Outputs "JohnDoe"
?>

It’s important to keep in mind that session data is stored on the server and not on the client side, so it can’t be accessed or modified directly by the user. This makes it a more secure way to store sensitive information such as login credentials, shopping cart contents or user preferences.

Session data is stored on the server in a temporary file, which is identified by the session ID. By default, session data is stored in the server’s file system, but it can also be stored in a database or other storage systems with the help of session handlers.

The session ID is usually stored in a cookie on the client side, but it can also be passed as a GET or POST parameter in the URL. This allows the session to be maintained across multiple pages and requests, even if the user closes the browser or turns off their computer.

The session_destroy() function can be used to end a session and delete all session data. This function should be called when the user logs out or when the session is no longer needed.
Here is an example of a login script that uses sessions to store the user’s login status:

<?php 
session_start();

if (isset($_POST["username"]) && isset($_POST["password"])) {
// Check the username and password against a database or other source
if ($_POST["username"] == "JohnDoe" && $_POST["password"] == "password") {
$_SESSION["logged_in"] = true;
$_SESSION["username"] = $_POST["username"];
header("Location: dashboard.php");
} else {
echo "Invalid username or password";
}
} else {
// Display the login form
echo '<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Log in">
</form>';
}
?>

In this example, the script first checks if the username and password fields have been submitted via the POST method. If they have, it checks the provided credentials.

In conclusion, sessions in PHP are a powerful and flexible tool for storing and managing user-specific information on the server side. They allow you to create personalized and consistent experiences for your users, while also keeping sensitive information secure. Sessions are easy to implement, with simple functions such as session_start(), $_SESSION, and session_destroy() that can be used to create, access and end a session respectively. They can also be stored in a database or other storage systems using session handlers. It's important to keep in mind that session data is stored on the server and not on the client side, so it can't be accessed or modified directly by the user. Overall, sessions in PHP are a valuable tool for any web developer looking to create dynamic and engaging web applications.

Get more valuable insights and tips by following me!
Also check my list of articles about the PHP or different type of architecture.

--

--