PHP - fonction session_unset ()

Définition et utilisation

Les sessions ou la gestion de session sont un moyen de rendre les données disponibles sur différentes pages d'une application Web. lesession_unset() La fonction libère toutes les variables des sessions en cours.

Syntaxe

session_unset();

Paramètres

Cette fonction n'accepte aucun paramètre.

Valeurs de retour

Cette fonction renvoie une valeur booléenne qui est TRUE si la session a démarré avec succès et FALSE sinon.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 4 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple 1

L'exemple suivant montre l'utilisation du session_unset() fonction.

<html>   
   <head>
      <title>Setting up a PHP session</title>
   </head>   
   <body>
      <?php  	
         //Starting a session	  
         session_start();   
         
         //Replacing the old value
         $_SESSION["A"] = "Hello"; 
         print("New value: ".$_SESSION["A"]);
         echo "<br>";		 
         print("Value of the session array: ");
         print_r($_SESSION);
         session_unset(); 
         $_SESSION = array();
         echo "<br>";
         print("Value after the reset operation: ");
         print_r($_SESSION);
      ?>
   </body>   
</html>

En exécutant le fichier html ci-dessus, il affichera le message suivant -

New value: Hello
Value of the session array: Array ( [A] => Hello )
Value after the reset operation: Array ( )

Exemple 2

Voici un autre exemple de cette fonction, ici nous avons deux pages de la même application dans la même session -

session_page1.htm

<?php
   if(isset($_POST['SubmitButton'])){ 
      //Starting the session	
      session_start();
      $_SESSION['name'] = $_POST['name'];
      $_SESSION['age']  = $_POST['age']; 
   }
?>
<html>
   <body>
      <form action="#" method="post">
         <br>
         <label for="fname">Enter the values click Submit and click on Next</label>
         <br><br><label for="fname">Name:</label>
         <input type="text" id="name" name="name"><br><br>
         <label for="lname">Age:</label>
         <input type="text" id="age" name="age"><br><br>           
         <input type="submit" name="SubmitButton"/>
         <?php echo '<br><br /><a href="session_page2.htm">Next</a>'; ?>
      </form>
   </body>
</html>

Cela produira la sortie suivante -

En cliquant sur Next le fichier suivant est exécuté.

session_page2.htm

<html>   
   <head>
      <title>Second Page</title>
   </head>
   <body>
      <?php
         //Session started
         session_start();	  
         //Changing the values
         $_SESSION['city'] = 'Hyderabad';
         $_SESSION['phone'] = 9848022338;
        
         print($_SESSION['name']); 
         echo "<br>";
         print($_SESSION['age']);
         echo "<br>";
         print($_SESSION['city']); 
         echo "<br>";
         print($_SESSION['phone']);
         echo "<br>";

         //Un-setting the values
         session_unset();
         print("Value of the session array: ");
         print_r($_SESSION);
      ?>   
   </body>   
</html>

Cela produira la sortie suivante -

krishna
30
Hyderabad
9848022338
Value of the session array: Array ( )