Session
Session
A normal HTML website will not pass data from one page to another.
(OR)
In other words, all information is forgotten when a new page is loaded. This
makes a quite problem for tasks like a shopping cart, which requires data (the
user's selected product) to be remembered from the time of selection to billing.
(i.e. one page to the next page).
Cookie used for such requirements, however, limitations on cookie size and
the number of cookies allowed, and various inconveniences surrounding their
implementation, prompted to introduce another solution called as session
handling.
A PHP session solves this problem by allowing you to store user information on
the server for later use (i.e. username, shopping cart items, etc). However, this
session information is temporary and is usually deleted very quickly after the user
has left the website that uses sessions.
11/30/2024
Open Source 1
SESSION Mechanism
There are two things that the session mechanism must hang onto:
the session ID itself and any associated variable bindings.
The session ID is either stored as a cookie on the browser's machine,
or it is incorporated into the GET/POST arguments submitted with
page requests.
The contents of session variables are stored in special files on the
server, one file per session ID:
Doing this kind of storage requires the session code to serialize the data
by turning it into a linear sequence of bytes that can be written to a file
and read back to recreate the data
It's possible to configure PHP to store the contents of session variables in
a server-side database, rather than in files
11/30/2024
Open Source 2
Storing the Sessions
Eg.
sess_076opqrstu56kldr670ndft0op
11/30/2024
Open Source 3
Working of a Session
11/30/2024
Open Source 4
Session Functions
11/30/2024
Open Source 5
Start a Session
Before you can begin storing user information in your PHP session, you
must first start the session. When you start a session, it must be at the very
beginning of your code, before any HTML or text is sent.
bool session_start (void)
Sample program to Start Session: Session_start.php
<?php
session_start(); // starts PHP session!
echo “The session was started”;
?>
This tiny piece of code will register the user's session with the server,
allow you to start saving user information and assign a UID (unique identification
number) for that user's session.
Open Source
Create a Session
When you want to store user data in a session use the $_SESSION (super global).
This is used for both store and retrieve session data.
$_SESSION[‘session_name’]
Sample program to Create Session: Session_Create.php
<?php
session_start(); // starts PHP session!
$_SESSION[‘view’] = 1; // creates a new session with name “view”
?>
11/30/2024
Open Source 7
Access Session
Can access the existing sessions through the following super global:
$_SESSION[‘session_name’] – global array
$_HTTP_SESSION_VARS[‘session_name’] – environment variables
<?php
11/30/2024
Open Source 8
Delete Session
Imagine that you were running an online business and a user used your website
to buy your goods. The user has just completed a transaction on your website
and you now want to remove everything from their shopping cart. Can delete
the existing session by the function “unset”. The sample program shows it:
Sample program to Delete Session: Session_Delete.php
<?php
session_start(); // starts PHP session!
if (isset($_SESSION[‘view’]) // checks the existing
{
unset($_SESSION[‘view’]); // deletes the existing
echo “The session ‘view’ was deleted”;
}
else
echo “no session with name ‘view’”;
?>
11/30/2024
Open Source 9
Delete All Sessions
Session_destroy() destroys all of the data associated with the current session. It
does not unset any of the global variables associated with the session. Variables
associated with the session, or unset the session cookie.
bool session_destroy (void)
Sample program to Delete all Session: Sessions_Delete.php
<?php
session_start(); // starts PHP session
……… //codings
session_destroy(); // deletes all sessions
?>
Note: destroy will reset your session, so don't call that function unless you are
entirely comfortable losing all your stored session data!
11/30/2024
Open Source 10
Register a
Session
session_register() accepts a variable number of arguments, any of which can be
either a string holding the name of a variable or an array consisting of variable
names or other arrays. For each name, session_register() registers the global
variable with that name in the current session.
bool session_register (mixed $name [,mixed $...])
Sample program to Register a Session: Session_Register.php
<?php
// Use of session_register() is deprecated
session_register("barney"); // barney can use later
?>
11/30/2024
Open Source 11
Use Registered
The registered session can beSession
used later. It is must to check a session is registered
or not, before use it. For that, session_is_registered is used to find whether a
global variable is registered in a session
11/30/2024
Open Source 12
Session
Name
session_name() returns the name of the current session.
bool session_name (string $name)
If name is given, session_name() will update the session name and return the
old session name. The session name is reset to the default value stored in
session.name at request startup time. Thus, you need to call session_name() for
every request (and before session_start() or session_register() are called).
11/30/2024
Open Source 13
Session id
session_id() is used to get or set the session id for the current session. The
constant SID can also be used to retrieve the current name and session id as a
string suitable for adding to URLs.
11/30/2024
Open Source 14