How to Handle session variables in Php
Session is nothing but way to store a details or information (using variables) in multiple pages. Its Not like as Cookie.
The difference is details are not stored in the computer browser as like as cookie.
What is PHP Session? – Simple and clear Explanation
When You open a page in the browser. It have a login page. you logged in. browse some pages, update some changes and do all thing and closed it. From Your computer, know this who you are, what you do, where is it. But from Internet or server don’t know about all things. because the HTTP address doesn’t maintain state.
Here session works. It stored all login details or any information using session variable. It will carry the details to all the pages, links, update contents and changes until you unset or destroy the session. which means until you log-out or close the browser.
Syntax – how to start a Session
session_start(); // Session syntax
Session Variables is globally declared as $_SESSION // variable syntax
Example
<?php session_start(); // Start the session sysntax $_SESSION["developer"] = "php "; // Session variable declared $_SESSION["desks"] = "Html"; // Session variable declared echo "We are learning " . $_SESSION["developer"] . ".<br>"; // Print session variable values echo "We are also learning " . $_SESSION["desks"] . "."; // Print session variable values ?>
Output :
We are learning php
We are also learning Hmtl
Here You can call the session variable where you want.
Print all session in single output
We can also Print all the Session in single array function
Syntax
<?php session_start(); // Start the session sysntax $_SESSION["developer"] = "php "; // Session variable declared $_SESSION["desks"] = "Html"; // Session variable declared print_r($_SESSION); ?>
output
Array ( [developer] => php [desks] => Html )
Modify the session variable
We can also modify the session variable where u want and when you want. It print last modified value only
<?php session_start(); // Start the session sysntax $_SESSION["developer"] = "php "; // Session variable declared $_SESSION["desks"] = "Html"; // Session variable declared $_SESSION["developer"] = "WELCOME"; // Simple modify the session variable echo "We are learning " . $_SESSION["developer"] . ".<br>"; // Print session variable values echo "We are also learning " . $_SESSION["desks"] . "."; // Print session variable values ?>
Output
We are learning WELCOME // Session variable print the last modified values
We are also learning Hmtl
Destroy or Unset the session variable
Want to remove all global session variables and destroy the session, we can use session_unset() and session_destroy():
Syntax:
<?php session_start(); // Start the session sysntax $_SESSION["developer"] = "php "; // Session variable declared $_SESSION["desks"] = "Html"; // Session variable declared $_SESSION["developer"] = "welcome"; session_unset(); // Session unset used to remove all the variables in the session session_destroy(); // Delete or End the session echo "We are learning " . $_SESSION["developer"] . ".<br>"; // Print session variable values echo "We are also learning " . $_SESSION["desks"] . "."; // Print session variable values ?>
If you run this code in your browser
Output
Notice: Undefined index: developer in you URL
We are learning .
Notice: Undefined index: desks in you URL
We are also learning .
Because variables are unset and destroyed.
Want to see the real time session variables functions work in multiple pages Click Here
Login page using session, user details with out stored in database
Like this session some related functions also in php they are like cookie() and local storage()