PHP Session Use, Application Example to start, destroy, unset, id

PHP Session

On the internet, the web server does not have any information which user visit the page or how many times. To track the information about the visitor or users which can visit the multiple web pages, during the accessing of multiple pages sessions are generated.

Caution: Session variables are stored on the web server, not on the client machine. The only sessionid will be stored on the client machine. IN every communication with the web server will contain session id in the HTTP header.

PHP Session Start

To start a session you have to use session_start() function. Session variables are store in the PHP super global $_SESSION variable.

Example – 1 session_start() function Code to start session

<?php
session_start();
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
		$_SESSION["food"] = "Italian";
		echo "Session variables are set." . "<br />";
		print_r ($_SESSION);
	?>
  </body>
</html>

In the above example, we create a session variable and stored in a $_SESSION variable.

PHP Session Start
Fig.1 – Start Session with session_start() function.

Note:You should have to write session_strat() function first.

How to read session variable

The below code is used to print the session value. The session store in the global $_SESSION variable.

Example – 1 Read session data with $_SESSION super global array variable

<?php
session_start();
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
		echo "Stored session value = " . $_SESSION["food"]
	?>
  </body>
</html>

In the above example, we print the value stored session variable .

PHP Session
Fig.2 – echo session variable from super global array.

Note:Every page need to have session_start() function to access the session variable in the variable.

How to Modify Session Variable

If you want to change the value of the session than you have to just write new the value of that session variable.

Example – 3 Edit or Modify Session Variable with $_SESSION super global array

<?php
session_start();
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
		$_SESSION["food"] = "Indian";
		echo "Session variables are set with the new value." . "<br />";
		print_r ($_SESSION);
	?>
  </body>
</html>

In the above example, we overwrite the session variable.

What is Session id in PHP
Fig.3 – Modification of session variables.

Destroy and Unset a PHP Session

If you want to only remove the session variable then you have to call the session_unset() function. But if you want to delete complete session than you have to call session_destroy() function.

Example – 4 Destroy and Unset Session with session_unset() and session_destroy() function

<?php
session_start();
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
	// To delete the session variable
		session_unset();
	
	// To delete the complete session
		session_destroy();
		
		print_r($_SESSION);
	?>
  </body>
</html>

In the above example, we delete the complete session.

PHP Session Destroy And Unset
Fig.4 – Session Destroy And Unset.

What is Session id in PHP

All the session variables or data stored in the web server itself. When you create a session in the PHP, the server create a PHPSESSID cookie variable and this cookie travel in the HTTP Header to recognize or identify the user.

In the session PHPSESSID cookie variable travel in the HTTP Header and all the data stored in the server, whereas in the cookie all variable travel in the HTTP Header and all the data stored in the client machine.

You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape