Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> PHP >> How to create and destroy session in php?
How to create and destroy session in php?

Session management is a mechanism to maintain state about a series of requests from the same user across some period of time. That is, the term "session" refers to the time that a user is at a particular web site.
Views: 1481     Comments: 0
by Arun Singh on 9/20/2011

PHP Session

Session management is a mechanism to maintain state about a series of requests from the same user across some period of time. That is, the term "session" refers to the time that a user is at a particular web site. The problem is that HTTP has no mechanism to maintain state. Individual requests aren't related to each other. The Web server can't easily distinguish between single users and doesn't know about user sessions. Session management refers to the way that associate data with a user during a visit to a Web page.

A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application. I.e. A session is a way to store information (in the form of variables) to be used across multiple pages. Unlike a cookie, specific variable information is not stored on the user’s computer. It is also unlike other variables in the sense that we are not passing them individually to each new page, but instead retrieving them from the session we open at beginning of each page.

Start Session:

Before you can store user information in your PHP session, you must first start up the session. session_start() function is used for start session into PHP page.

Syntax:

?php

// Start session 

session_start();

?>

<html>

    <head>

        <title></title>

    </head>

    <body>

       

    </body>

   

</html>

Destroying Session:

There are basically two functions which is for destroying Session, first one is unset() function and second one is session_destroy() .

The unset() function is used to free specified session variable.

Example: Using unset function

 <?php

// Start Session

session_start();

// check session value has been set or not

$_SESSION['TestUnset'] ="Unset";

?>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title>PHP Session</title>

    </head>

    <body>

        <div id ="viewContainer">

            <h1> This is session page testing </h1>

        <?php

        // dispaly number of visitor in  page

           echo 'Session Value is :  '.$_SESSION['TestUnset']."</br>";

           unset ($_SESSION['TestUnset']);

           echo 'Session Destroyed'."</br>";

           echo 'Now Session Value is :  '.$_SESSION['TestUnset'];

        ?>

       </div>

    </body>

</html>

Example: using session_destroy function

<?php

// Start Session

session_start();

// check session value has been set or not

$_SESSION['TestUnset'] ="Unset";

?>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title>PHP Session</title>

    </head>

    <body>

        <div id ="viewContainer">

            <h1> This is session page testing </h1>

        <?php

        // dispaly number of visitor in  page

           echo 'Session Value is :  '.$_SESSION['TestUnset']."</br>";

           session_destroy() ;

           echo 'Session Destroyed'."</br>";

           echo 'Now Session Value is :  '.$_SESSION['TestUnset'];

        ?>

       </div>

    </body>

</html>

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 3231
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.