15 December 2013

Session Handling in PHP

Introduction

Session is the way to handle client connection at the server side. For establish communication with server, client need to register their self by session. Once the session has created, server will be able to listen the client request. In PHP we can create session eaisly by using single function.

Session creation

Syntax:
<?php
session_start();
$_SESSION[id]=value;
?>
we can use this session variable in another page by using this global session variable. For access session variable, we can use general method for using a variable.
<?php
$val=$_SESSION[id];
?>
Example:
<?php
session_start();
$_SESSION['userID']=”123456″;
?>

Destroy a session

Destroy a session is needed, because when client request is completed client can manually destroy the session or server can destroy the session after specified time.
Syntax:
<?php
require(“config.php”); // require which page session you have to destroy
session_destroy();
exit();
?>
For better understanding you can download a login interface with session control:Session.rar (5 KB)

0 comments:

Post a Comment

Thank you for comment. We will try to enhance the quality of this website.