Session in PHP

Abhigyan Singh 12th Jul 2020

The main drawback of HTML is, It cannot send data from one page to another page. When a new page is loaded, all the value or data from previous page removed and new page loaded. So it is not helpful in today’s E-Commerce like shopping cart, online banking etc. where we need to maintain the all through all pages.

So to remove or overcome these drawbacks, PHP introduced a new concept called SESSION. Session is used to store data on the server for future use. This stored data in temporary.  When user closed the session,  close the browser or close the site, the session will get destroyed.
It is widely used in E-Commerce, online banking etc.
Example: In E-Commerce website, When user logged in then the detail of the particular user is stored in a session and user can user that data entire the page while log out the data will destroyed.
For using the concept of session, We need three steps,
1) Start Session
2) Store value into the session
3) Destroy/ Unset  the session
1) Create session
To start session, we need to write
Session_start();
It should be the first line of your code before any HTML or text request. While writing this line session will get start.
2) Store value into the session
Now , we can store value into this session
Session_start();
$_SESSION[‘value’] = “discussdesk”;
This says that discussdesk has stored in session called value. Now this value can be used entire the page.
3) Destroy/ Unset  the session
Destroying the session or unset session is different
Destroying the session means it destroys all the session on that browser.
Session_destroy();
Session_start();
Session_destroy();
Unset session means It unset the session for particular site.
Unset($_SESSION[‘value’]);
Session_start();
If(isset($_SESSION[‘value’]))
Unset($_SESSION[‘value’]);

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

ALSO ON DISCUSS DESK