Session Handling - WORD
Session Handling - WORD
• The problem with this, is there is no way of consistency or personalization on the web,
because there is no way to know who is sending the requests.
• when a user visits a web site, the server stores information about the user in a cookie and
sends it to the browser, which saves it.
• Cookies are files residing on the CLIENT'S computer that store variables set by a particular
website.
• The problem with cookies is that they are an untrusted medium. Users can modify cookie
data, and cause unwanted problems with your app
• Sessions are a lot like cookies, however they reside on the SERVER machine, and cannot
be edited directly by the client.
• When you use sessions, a session ID is stored either in a cookie on the client side, or in some
sort of storage on the server side (with an HTTP cookie holding only the session ID so the
server can identify the client).
• One important issue which confounds to the session_start() function involves exactly where
this function can be called.
• In this section we are going to see the key session-handling tasks, such as creation and
destruction of a session, setting and retrieval of the SID, storage and retrieval of session
variables.
Starting a Session:
• In order to work with session first of all we need to start the session by declaring the
session_start( ) function at the top of the page as
<?php
session_start();
?>
• Once you start the session, you can now start using session variables
• session_start() creates a new session if no SID is found, or continue a current session if an
SID exists.
• When session_start() is first called, PHP sets a cookie (yes, a cookie) in your visitor's browser,
containing a session identifier ("session ID").
• It also creates a session data file to store variables related to that particular session.
• Notice that session variables are not passed individually to each new page, instead they are
retrieved from the session we open at the beginning of each page (session_start()).
• Also notice that all session variable values are stored in the global $_SESSION variable:
• Session variables are used to store and carry data from one page to another.
• Session variables are set with the PHP global variable: $_SESSION.
page1.php
<?php
session_start();
$_SESSION[„name‟]=“Asok”;
?>
page2.php
<?php
session_start();
?>
• To delete a session variable
Although you can configure PHP‟s session-handling directives to automatically destroy a session
based on an expiration time or garbage collection probability, sometimes it‟s useful to manually
cancel out the session yourself. For example, you might want to enable the user to manually log out
of your site. When the user clicks the appropriate link, you can erase the session variables from
memory, and even completely wipe the session from storage, done through the
session_unset()
session_destroy()
functions, respectively.
The session_unset() function erases all session variables stored in the current session, effectively
resetting the session to the state in which it was found upon creation (no session variables registered).
Eg:
<?php
session_start();
unset($_SESSION['A']);
echo "</br>";
echo $_SESSION['B'];
exit;
?>
Destroying a Session:
Or
• session_unset();
– This function will delete all session variables stored in the current session, but it will
not completely remove the session from the storage mechanism
• session_destroy();
– This function will completely destroy the session, by removing the session from the
storage mechanism
• session_encode() offers a convenient method for manually encoding all session variables into
a single string.
• This function is particularly useful when you „d like to easily store a user‟s session
information within a database,
<?php session_start();
$_SESSION[„username‟]=“Asok”;
//encode all session data into a single string and return the result
$sesval= session_encode();
echo $sesval;
?>
• This function will decode the variables, returning them to their original format.
• If the decoding is successful , it returns true(1) otherwise false(0)
• i.e.,
session_decode($sesval);
echo $_SESSION[„username‟];
session_id([string sid])
The function session_id() can both set and get the SID. If it is passed no parameter, the function
session_id() returns the current SID. If the optional SID parameter is included, the current SID will be
replaced with that value. An example follows:
<?php
session_start();
echo "Your session identification number is " . session_id();
?>
output
This results in output similar to the following:
Your session identification number is 967d992a949114ee9832f1c11c
session_regenerate_id([boolean delete_old_session])
The optional delete_old_session parameter determines whether the old session file will also be
deleted when the session ID is regenerated. By default, this behavior is disabled.