Session in PHP

Session in PHP


Posted in : PHP Posted on : January 6, 2011 at 6:36 PM Comments : [ 0 ]

This section contains the detail about Session in PHP .

Session in PHP:

The php session provide a way to track the logged user and store the user related information for later use. The session information is temporary and will be deleted after the user has left the website. If you want to store the user information in the session, you must first start up the session. In this example we will start the session. The session_start() function must appear BEFORE the <html> tag.

Example:

<?php session_start(); ?>
<html>
.......
.......
</html>

If you want to store the logged user information then the correct way to use $_SESSION. In this example you can see how to store the temporary values in the session  and how to retrieve these information.

Example:

<?php 
session_start(); 
// store session data
$_SESSION['user_name']="Avanish";
$_SESSION['email']="avanish@gmail.com";
?>
<html>
<body>
<?php
//retrieve session data
echo "User Name :". $_SESSION['user_name']."<br/>";
echo "Email :". $_SESSION['email'];
?>
</body>
</html>

Output:

User Name :Avanish
Email :avanish@gmail.com

If user want to store register global variables with the current session then use session_register() method and unregister variables using session_unregister() method.

Example:

<?php 
session_start(); 
// store session data

$_SESSION['user_name']="Avanish";
$_SESSION['email']="avanish@gmail.com";

session_register('email_id', 'avanish@gmail.com');
?>
<html>
<body>
<?php
if(session_is_registered('email_id')){
//retrieve session data before session unregister
echo "User Name :". $_SESSION['user_name']."<br/>";
echo "Email :". $_SESSION['email'];
}
session_unregister('email_id');

if(session_is_registered('email_id')){
//retrieve session data after session unregister
echo "User Name :". $_SESSION['user_name']."<br/>";
echo "Email :". $_SESSION['email'];
}
?>
</body>
</html>

 

Output:

User Name :Avanish
Email :avanish@gmail.com

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

The unset() function is used to free the specified session variable.

Example:

<?php 
session_start(); 
// store session data

$_SESSION['user_name']="Avanish";
$_SESSION['email']="avanish@gmail.com";
?>
<html>
<body>
<?php

//retrieve session data before session unset
echo "User Name and email before session unset.<br/>";
echo "User Name :". $_SESSION['user_name']."<br/>";
echo "Email :". $_SESSION['email']."<br/>";

// session unset
unset($_SESSION['user_name']);
unset($_SESSION['email']); 

//retrieve session data after session unset
echo "User Name and email after session unset.<br/>";
echo "User Name :". $_SESSION['user_name']."<br/>";
echo "Email :". $_SESSION['email'];

?>
</body>
</html>

Output:

User Name and email before session unset.
User Name :Avanish
Email :avanish@gmail.com
User Name and email after session unset.

Notice: Undefined index: user_name in Session.php on line 23
User Name :

Notice: Undefined index: email in Session.php on line 24
Email :

You can also completely destroy the session by calling the session_destroy() function:

Example:

<?php 
session_start(); 
// store session data
$_SESSION['user_name']="Avanish";
$_SESSION['email']="avanish@gmail.com";
?>
<html>
<body>
<?php

//retrieve session data before session destroy.
echo "User Name and email before session destroy.<br/>";
echo "User Name :". $_SESSION['user_name']."<br/>";
echo "Email :". $_SESSION['email']."<br/>"; 
// session destroy
session_destroy(); 
?>
</body>
</html>

Output:

User Name and email before session destroy.
User Name :Avanish
Email :avanish@gmail.com

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics