session in php

session in php

A php session variable is used to stare the data or information of single user and are available for all pages in one application. It is storing all the information and data for temporary use. It is automatically deleted when you close application or remove cash memory from your browser.
Sessions work by creating a Unique Id (UID) for each single visitor and stores variable base information on this UID. In this UID either stores cookies or information like username, shopping cart items, etc.

How to define session??

Before you can begin storing information in session, we need to start session first. It must be at the beginning of the code or before any HTML or text.

Below is a simple script to start up a PHP session. You must need to place it at the beginning of the code.

Example:

<?php 
session start(); //start-up of php session
?>
How to define session variable??

If you want to store any user information or any other information first you need to define session variable using bellow syntax. This syntax is used in both stored or retrieve session data which is stored previously.

Example:

<?php 
session_start(); //session start-up
$_SESSION[‘username’]= Narendra Modi; //Store session data
echo “Username is”. $_SESSION[‘username’]; // Retrieve session data
?>
How to delete session??

To delete a session variable, you just need to site below syntax along with session name.

Example:

<?php
session_start(); //session start-up
$_SESSION[‘username’]= Narendra Modi; //Store session data
unset($_SESSION["username’"]); //Delete session data
?>

That’s all. If you find this article is helpful don’t forget to share with your friends. Please help us by doing comment using comment box. Thank you!





Also Read:

How to run multiple insert query in SQL using PHP in one go?

Post Comments(0)

Leave a reply