• PHP Video Tutorials

PHP - session_register_shutdown() Function



Definition and Usage

Sessions or session handling is a way to make the data available across various pages of a web application. The session_register_shutdown() function registers session_write_close() as a session shutdown function, i.e. you can shut down a function by invoking these both functions at once.

Syntax

session_register_shutdown();

Parameters

This function does not accept any parameters.

Return Values

This function doesn’t return any values.

PHP Version

This function was first introduced in PHP Version 4 and works in all the later versions.

Example 1

Following example demonstrates the usage of the session_register_shutdown() function.

<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("Value of the session array: ");
         print_r($_SESSION);
         
         //Shutting down the session
         session_register_shutdown(); 
         session_write_close();
         echo "<br>";
         print("Value after the session shut down: ");
         print_r($_SESSION);
      ?>
   </body>   
</html> 

One executing the above html file it will display the following message −

Value of the session array: Array ( [A] => Hello )
Value after the session shut down: Array ( [A] => Hello ) 

Example 2

Following is another example of this function, in here we have two pages from the same application in the same 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>

This will produce the following output −

Session starts

On clicking on Next the following file is executed.

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;
       
         //Shutting down the session
         session_register_shutdown();
         session_write_close();
         print_r($_SESSION);
      ?>   
   </body>   
</html>

This will produce the following output −

Array ( 
   [A] => Hello [name] => krishna 
   [age] => 30 [city] => Hyderabad 
   [phone] => 9848022338 
)
php_function_reference.htm
Advertisements