PHP - session_encode() 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_encode() function encodes the data in the session into an encoded string and returns it.

Syntax

session_encode();

Parameters

This function does not accept any parameters.

Return Values

This function encodes the data in the current session and returns it in the form of encoded serialized string.

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_encode() function.

   
   
      Setting up a PHP session
      
   
      
      

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

Encoded Data: data|s:19:"This is sample data";

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


   
      








Next'; if(isset($_POST['SubmitButton'])){ //Starting the session session_start(); $_SESSION['name'] = $_POST['name']; $_SESSION['age'] = $_POST['age']; $res = session_encode(); echo "

Encoded Data: ". $res; } ?>

This will produce the following output −

Session start

After clicking on the Submit button the above page looks like −

Session encode

On clicking on Next the following file is executed.

session_page2.htm

   
   
      Second Page
   
   
         
      

This will produce the following output −

Encoded Data: data|s:19:"This is sample data";name|s:7:"Krishna";age|s:2:"30";City|s:9:"Hyderabad";Phone|s:10:"9848022338";
php_function_reference.htm
Advertisements