0% found this document useful (0 votes)
61 views

SESSION in PHP

The document discusses PHP sessions. It explains that sessions allow servers to track information about users across multiple pages by storing data on the server. Sessions start with the session_start() function, which loads session data into the $_SESSION superglobal array. Data can then be stored in and retrieved from $_SESSION. Sessions end when session_destroy() is called or after a period of inactivity defined by the session timeout.

Uploaded by

Joy Pal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

SESSION in PHP

The document discusses PHP sessions. It explains that sessions allow servers to track information about users across multiple pages by storing data on the server. Sessions start with the session_start() function, which loads session data into the $_SESSION superglobal array. Data can then be stored in and retrieved from $_SESSION. Sessions end when session_destroy() is called or after a period of inactivity defined by the session timeout.

Uploaded by

Joy Pal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Presented by

Syed Abdullah - 201002263

JOY PAL - 201002418

Ananda Sarkar Naba - 201002038


SESSION
in PHP
TABLE OF CONTENTS

01
PH 02 03
P Session Session
Short Review Why, where
Fundamental
session is used? Start a session
How its work? Ending a session
Session timeout etc.
3
PHP
Hypertext Preprocessor

 server-side scripting language

 a powerful tool for making dynamic and


interactive Web pages.

 PHP is a widely-used, free, and efficient


alternative to competitors such as
Microsoft's ASP.

4
Session in PHP

 Generally session means limited period of time or a


period devoted to a particular activity.
 There is no continuous connection between client and
server.
 Allow to track information of user.

 A browser request a page from server – connection


opens between both, server get the request and sends
back the response – connection closed

5
Why & Where Session is
Used ?

 Stored the information is a hidden form field,


then it persist only if the form is submitted.
 To make data accessible across the various
pages of an entire website.

 Suppose a user is buying book online, there


should be a mechanism to keep track of books
added by user to basket.

6
How Session Works

 Session is a file stored on server.


 Client’s browser makes an initial request to the server.
 The browser sends a request to the server. PHP responds by
sending a unique token that identifies the current session.
 server notes client's IP address/browser, stores some local
session data, and sends a session ID back to client (as a
cookie)
 server uses session ID cookie to retrieve its data for the
client's session later

7
PHP Session Working (example)

8
Start a PHP Session

o A Session is started with the session_start( ) function.

o call session_start ( ) :
 if the server hasn't seen this user before, a new session is
created
 otherwise, existing session data is loaded
into $_SESSION associative array
 you can store data in $_SESSION and retrieve it on future pages

9
Start a PHP Session (example)
<?php
session_start(); // Start the session
?>
 The session_start() functio
<!DOCTYPE html>
n must be the very first
<html>
thing in your document.
<body>
Before any HTML tags.
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
1
Accessing session data

$_SESSION["name"] = value; # store session data


$variable = $_SESSION["name"]; # read session data
if (isset($_SESSION["name"])) { # check for session data

if (isset($_SESSION["points"])) {
$points = $_SESSION["points"];
print("You've earned $points points.\n");
} else {
$_SESSION["points"] = 0; # default

11
Ending a session
 To remove all global session variables and destroy the session,
use session_unset() and session_destroy()

 session_destroy ends your current session

 if you may want to start a completely new empty session later, it is best
to flush out the old one

session_destroy();
session_regenerate_id(TRUE); # flushes out session #ID number
session_start();
1
Ending a session (example)

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
session_unset(); // remove all session variables
session_destroy(); // destroy the session
?>
</body>
</html>
1
Session Timeout

• Because HTTP is stateless, it is hard for the server to know when a user
has finished a session
• ideally, user explicitly logs out, but many users don't
• server automatically cleans up old sessions after a period of time
• old session data consumes resources and may present a security risk
• adjustable in PHP server settings or with session_cache_expire
 function
• you can explicitly delete a session by calling session_destroy

1
THANK
S!

You might also like