What is the PHP session and how to store the value into the PHP session?

To store values into the container we mostly use the variable , while the variables have maximum scope to the current page only . So we use sessions to pass the values from one page to another in hidden format. We can also pass the values from one page to anther in the URL parameters GET and POST but security purposes we choose the PHP SESSION . There is no need to pass the sessions from one page to another page again and again. We just set the sessions value one time then we can easily fetch this value from any where within the current host server/domain.

Syntax of PHP Session

session_start(); $_SESSION['sessionName'] = 'Some Value';

Remember that it is compulsory to call the session_start() function on the page where we want to set and get the SESSION value.

How to set and Get the PHP Session?

session_start(); $_SESSION['username'] = 'Codingpk'; // SESSION SET echo $_SESSION['username']; // SESSION GET

Output of above SESSION


Destroy SESSION Syntax

session_destroy(); // Destroy Function OR unset($_SESSION['username']); // Destroy individually
php PHP Sessions Chapter Next »