How to Create a PHP Session

PHP session is a way to store data in variables that can be accessed on all webpages.

Follow the steps below to create and use a session in PHP:

1.     Start the Session

To start a session, we use session_start() at the top of our file. This makes the session available for storing and retrieving data.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
session_start(); // Start the session
?>
<?php session_start(); // Start the session ?>
<?php
session_start();  // Start the session
?>

2. Store Data in the Session

Now we store data in the session. To store data in session, we use  $_Session array.

For example, to store a username in a session

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
session_start();
$_SESSION['username'] = 'Karan';
?>
<?php session_start(); $_SESSION['username'] = 'Karan'; ?>
<?php
session_start();  
$_SESSION['username'] = 'Karan';
?>

3. Get Data from the Session

Now, to get the username from the session, we use $_SESSION with the key, which is the username.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
session_start();
// Get and show the stored username
echo 'User name is ' . $_SESSION['username']; // Output: User name is , Karan
?>
<?php session_start(); // Get and show the stored username echo 'User name is ' . $_SESSION['username']; // Output: User name is , Karan ?>
<?php
session_start(); 
// Get and show the stored username
echo 'User name is  ' . $_SESSION['username'];  // Output: User name is , Karan
 ?>

4. Update session data

Now, to update session data, we simply pass a new value to the session variable; it will remove the old value and save the new value in the session variable. Now we update the username to Mohit.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
session_start();
// Change the username
$_SESSION['username'] = 'Mohit';
?>
<?php session_start(); // Change the username $_SESSION['username'] = 'Mohit'; ?>
<?php
session_start();  
// Change the username
$_SESSION['username'] = 'Mohit';
?>

5. Remove or Destroy Session Data

To remove data from a session, we use unset(), which will remove the session variable, and to destroy the session, we use session_destroy().

Remove a Session Variable:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
session_start();
// Remove the username from the session
unset($_SESSION['username']);
?>
<?php session_start(); // Remove the username from the session unset($_SESSION['username']); ?>
<?php
session_start();  
// Remove the username from the session
unset($_SESSION['username']);
?>

Destroy the entire session completely

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
session_start();
// Remove all session variables
session_unset();
// Destroy the session
session_destroy();
?>
<?php session_start(); // Remove all session variables session_unset(); // Destroy the session session_destroy(); ?>
<?php
session_start();  
// Remove all session variables
session_unset();
// Destroy the session
session_destroy();
?>

Summary

PHP allows you to store data on the server that can be accessed across multiple pages on a website. This is useful for things like user logins, shopping carts, and saving preferences. By using session_start(), you can store, retrieve, and manage data easily throughout a user’s visit to your site.

Read Also :
Understanding the wp_head Hook in WordPress

Understanding the_content Filter Hook in WordPress: Customizing Post Content

 

Also Visit :
https://inimisttech.com/

 

Leave a Reply