Open In App

PHP | Unset Session Variable

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Whenever data are stored using cookies, there is a possibility of a hacker to insert some harmful data in the user’s computer to harm any application. So its always advisable to use PHP sessions to store information on the server than on a computer. Whenever data is needed across many pages of a website or application, PHP Session is used.

This session creates a temporary file in a folder that stores registered variables with their values which are made available for the entire website. This Session ends when the user logs out of the site or browser. Every different user is given unique session IDs that are linked to an individual’s posts or emails.

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

Note: The session IDs are randomly generated by the PHP engine which is difficult to make out.

Example: The following PHP function unregisters or clears a session variable whenever $_SESSION is used in some code. It is mostly used for destroying a single session variable.

  • Syntax:
      unset($_SESSION['variable_name']);
    
  • Program 1 :




    <!DOCTYPE html>
    <html>
      
    <head>
        <style>
            body {
                width: 450px;
                height: 300px;
                margin: 10px;
                float: left;
            }
              
            .height {
                height: 10px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color:green">GeeksforGeeks</h1>
        <b> PHP Unset session variables </b>
        <div class="height"> </div>
    <?php
         
    // start a new session 
    session_start(); 
            
    // Check if the session name exists 
    if( isset($_SESSION['name']) ) { 
        echo 'Session name is set.'.'<br>'; 
    else { 
        echo 'Please set the session name !'.'<br>'; 
     echo'<br>';
     $_SESSION['name'] = 'John'; 
       
      //unset($_SESSION['name']);     
     echo "Session Name : ".$_SESSION['name'].'<br>'; 
        
    ?> 
    </body>
    </html>

    
    

  • Output: When we comment on unset($_SESSION[‘name’]), then you get the following output .

Note: The PHP session_start() function is always written in the beginning of any code.

  • When we do unset($_SESSION[‘name’]), by un-commenting the required line in the example program, you get the following output.
      unset($_SESSION['name']);      
      //echo "Session Name : ".$_SESSION['name'];  
    
  • Output:
  • If you want to destroy all the session variables, then use the following PHP function.
    session_destroy();
  • If you want to clear or free up the space occupied by session variables for other use, the following PHP function is used.
    session_unset();
  • Program 2:




    <!DOCTYPE html>
    <html>
       
    <head>
        <title>Unset Session Variable </title>
    </head>
       
    <body>
        <h1 style="color:green">GeeksforGeeks</h1>
        <b> Unset previous session of user</b>
        <?php
         echo '<br>';
         echo '<br>';
         if(isset($_SESSION["user_name"]))
         { echo "Welcome "; echo $_SESSION["user_name"]; }
        ?>
            <form>
                Input your name here:
                <input type="text" id="user_id" name="user_id">
                <input type=submit value=Submit>
            </form>
            <form action="#">
                <input type="submit" name="submit"
                       value="Unset"
                       onclick="UnsetPreviousSession()">
            </form>
        <?php 
       
        session_start();
       
        if(!isset($_SESSION["user_name"]) && (!empty($_GET['user_id'])))
        {
            $_SESSION["user_name"] = $_GET["user_id"];
        }
        else
        
            UnsetPreviousSession();
        }
        function UnsetPreviousSession()
        {
           unset($_SESSION['user_name']); 
        }
        ?>
    </body>
       
    </html>

    
    

  • Output: After clicking the Unset button, the following output screen is visible for re-entry which shows the previous session variable is unset.


Last Updated : 20 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads