0% found this document useful (0 votes)
26 views14 pages

Session

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views14 pages

Session

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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

 The session variables are stored on the web server


 The Path is /var/lib/php/session
 The file name is the session ID

Eg.
sess_076opqrstu56kldr670ndft0op

11/30/2024
Open Source 3
Working of a Session

Sessions work by creating a unique identification(UID) number for each visitor


and storing variables based on this ID. This helps to prevent two users' data from
getting confused with one another when visiting the same webpage.
Session_start() – it is required, for every session’s program.
$_SESSION[‘session_name’] – can access the created sessions through
this super global.
isset($_SESSION[‘session_name’]) – used to check the availability of
a session.
unset($_SESSION[‘session_name’]) – deletes the session
Session_destroy() – deletes all sessions.

11/30/2024
Open Source 4
Session Functions

session_start — Initialize session data.


session_destroy — Destroys all data registered to a session
session_unset — Free all session variables
session_name — Get and/or set the current session name
session_register — Register one or more global variables with the
current session.
session_destroy — Destroys all data registered to a session.
session_id — Get and/or set the current session id.
Session_regenerate_id — Update the current session id with a newly
generated one
session_is_registered — Find out whether a global variable is
registered in a session

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

Sample program to Access a Session: Session_Access.php

<?php

session_start(); // starts PHP session!


if (isset($_SESSION[‘view’]) // checks the availability
$_SESSION[‘view’] += 1; // access the existing
?>

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

bool session_is_registered (string $name)

Sample program to use Registered Session: Session_Registerd_use.php


<?php // use the registered sesssion
session_start();
if (session_is_registered(‘barney’])
echo "barney is ",$_SESSION['barney']++."<br>";
else
echo “The session ‘barney’ is not yet registered”;
?>

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).

Sample program for Session Name: Session_Name.php


<?php
// set the session name to WebsiteID
$previous_name = session_name("WebsiteID");
echo "The previous session name was $previous_name<br />";
?>

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.

bool session_id (string $id)


The session id can be updated by the function
bool session_regenerate_id ([bool $delete_old_session=false])

Sample program for Session Name: Session_Name.php


<?php
session_start();
echo "session id is ".session_id()."<br>";
echo "barney is ",$_SESSION['barney']++."<br>";
?>

11/30/2024
Open Source 14

You might also like