Session in PHP – Session start destroy

What is Session in php ?
A session is a way to store information (variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

  • php Session is way to store information and this information will be available to all pages in one application. But when close browser the session information will destroy.
  • How to create session?
  • For maintaing session firstly call session_start() function. Because session_start() start session. Then create session variable like
  • $_SESSION[‘username’]=’shishir012010′;
  • here $_SESSION[‘username’] is a session variable and shishir012010 is information that store in $_SESSION[‘username’] variable.

Basic PHP Syntax
Example 1:- To create session

<?php

session_start();
$_SESSION['username']='shishir012010';
?>

Example 2:- To access session

<?session_start();
echo $_SESSION['username'];
?>.

Example 3:- To delete session

<?php
session_start();
unset($_SESSION['username']);
?>

Note- The unset function is use delete one session variable
Example 4:- To delete all session variable

<?php
session_start();
session_destroy();
?>

Note:- session_destroy() function delete all session variable.

Session_id()
session_id() function returns the session id for the current session.

Example 6:-

<?php
session_start();
echo session_id();
?>

Note:- we use session_id() for maintaining shopping cart.

Session variables solve this problem by storing user information to be used across multiple pagesBy default, session variables last until the user closes the browser.Session variables hold information about one single user, and are available to all pages in one application.

			

Admin has written 171 articles

Leave a Reply