How to start a PHP session, store and accessing Session data?

By Faruque Ahamed Mollick

What is PHP session?

Session in PHP is a way of storing information or data inside variables to be used in multiple pages of a web application. You can compare PHP session with the cookie, but the session is the more secure way and it does not store information on users computers. Session values are stored on the server.

You can see login and logout system on many PHP based websites or web applications. This task is done by using PHP session. A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.



How to start a PHP session?

Starting a session in PHP is so simple and easy. Below is the given code to start a session in PHP:

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

The PHP session_start() function checks for an existing session ID first. If it finds one, i.e. if the session is already started, it sets up the session variables and if doesn’t, it starts a new session by creating a new session ID.

How to store session data in PHP session?

Storing a session data is also so easy task. Below is the simple code which shows you how to store information or data in PHP session:

<?php
session_start();
$_SESSION["user_name"] = "mike";
$_SESSION["user_id"] = 1234;
?>

Or you can stake the value first in PHP variable and this task can be done with the below code:

<?php
session_start();
$username = "mike";
$userid = 1234;

$_SESSION["user_name"] = $username;
$_SESSION["user_id"] = $userid;
?>

 Accessing Session Data

Easy URL Shortener With Analytics – PHP MySQL

Suppose you want to display session data on your web page. So what to do? Well, it is so simple. Below is an example which will display the session data that we store using the above code on your web page:

<?php
// Starting a session
session_start();
 
// Accessing the session data
echo 'Hi, ' . $_SESSION["user_name"] . ' with id ' . $_SESSION["user_id"];
?>

 Destroy session data

If you want to destroy all session data, then below is the given code which will do that:

<?php
// Starting PHP session
session_start();
 
// Destroying all session data on server
session_destroy();
?>

The session_destroy() PHP function has used to destroy all the session data from the server. It can be used to build log out system to make it enable for a user to logout from a website that is built in PHP programming language.

YouTube Video Downloader PHP Script – Download Source Code

However, you may want to destroy only particular session data. Below is the example code which will destroy particular session data:

<?php
session_start();
 
// Destroy a particular session data
if(isset($_SESSION["user_id"])){
    unset($_SESSION["user_id"]);
}
?>

The above code will remove the user id from the session, but username will still exist.

Leave a Reply

Your email address will not be published. Required fields are marked *