Home >>Advance PHP Tutorial >PHP Session Variables

PHP Session Variables

Use of Session and Cookie in PHP

You already know that the Hypertext Transfer Protocol(HTTP) is the standard protocol used to transfer data between your browser and the various Web sites you visit.

What you may not know, however, is that HTTP is a "Stateless" Protocol, which treats each request for a Web page as a unique and independent transaction, with no relationship.

To work around this problem, most Web sites use cookies or sessions to "maintain state," in order to offer enhanced services.

for example, shopping carts or automatic restoration of personal settings -- to site users.

Why Session is more secure than cookies

You know what cookies are: text files stored on a user's system that help a Web site recognize the user and retrieve specific information about him or her.

The problem with cookies is that they're not very secure: because they are stored on the client, it's possible for any user to open the cookie file and read or modify the information stored within it.

That's why many Web sites prefer to use sessions . sessions work much like cookies, except that the information used to maintain state is stored on the server, rather than on the client.

Creating Session environment and Session Variables

In a Session-based environment, every client is identified through a unique number called session identifier and this unique number is used to link each client with its information on the server.

Every time the client visits the Web site the site reads the client's session identifier and restores state information.

The session_start( ) function is used to start session environment.

Session variable always works its own(session) environment or its associated environment

Note : The session_start( ) function must appear BEFORE the <html> tag. Try to use session_start( ) at top of the PHP script.

<?php
	 
session_start();
  
?>

	<html>
	
 <body>
	
	
 </body>

	</html>

Storing a Session Variable

To store and retrieve session variables use the $_SESSION [ ]. save it stroreSession.php

<?php
	
//first start session environment

session_start(); 
	
// store data in session variable through user

$_SESSION['user']= $_POST['un'];

$_SESSION['profile']= $_POST['prof'];  

?>

<html>
 
<body>

	<form method="post">

		Enter your user name <input type="text" name="un"/><hr/>

		Enter your profile  <input type="text" name="prof"/><hr/>

		<input type="submit" value="Store in session variable"/><

	</form>

 </body>

</html>


Output :
check output on retrieveSession.php
Enter your user name
Enter your profile

Retrieving a Session Variable

To retrieve session variables first start session environment. save it retrieveSession.php
<?php	

//first start session environment
 session_start(); 
	
// retrieve session data
	    
echo "Welcome ".$_SESSION['user']."<br/>";
	
echo "Your profile is ".$_SESSION['profile'];  
 
?>

Output :
Welocme abhi
Your profile is developer

Destroying a Session Variable

If you want to delete some session data, you can use the unset( ), session_unregister( ) or the session_destroy( ) function.

The unset( ) and session_unregister( ) function is used to free the specified session variable. while session_destroy( ) function completely destroy the session environment.

save it stroreSession.php

<?php
	
//first start session environment

session_start(); 
	
// store data in session variable through user
	    
$_SESSION['user']= $_POST['un'];

$_SESSION['profile']= $_POST['prof'];
		
//destroy one session variable(individual) use
unset($_SESSION['user']);

//OR
	
//session_unregister('user');	
		
//destroy complete session environment use
//session_destroy();

?>

<html>

 <body>

	<form method="post">

		Enter your user name <input type="text" name="un"/><hr/>

		Enter your profile  <input type="text" name="prof"/><hr/>

		<input type="submit" value="Store in session variable"/><

	</form>

 </body>

</html>

Output : check output on retrieveSession.php
Enter your user name
Enter your profile

retrieveSession.php

<?php	
	    
session_start(); 
	 
// retrieve session data
	    
echo "Welcome ".$_SESSION['user']."<br/>";
	
echo "Your profile is ".$_SESSION['profile'];  

?>

Output : Welocme Your profile is developer

No Sidebar ads