SlideShare a Scribd company logo
PHP - Introduction to PHP Cookies and Sessions
Introduction to PHPIntroduction to PHP
Cookies & SessionsCookies & Sessions
So…So…
Cookies Sessions
Limited storage space Practically unlimited
space
Insecure storage client-
side
Reasonably securely
stored server-side
User controlled No user control
How do ‘Sessions’ work?How do ‘Sessions’ work?
• They are based on assigning each user a unique
number, or session id. Even for extremely heavy use
sites, this number can for all practical purposes can
be regarded as unique.
e.g.
26fe536a534d3c7cde4297abb45e275a
How do ‘Sessions’ work?How do ‘Sessions’ work?
• This session id is stored in a cookie, or passed in the
URL between pages while the user browses.
• The data to be stored (e.g. name, log-in state, etc.)
is stored securely server-side in a PHP superglobal,
and referenced using the session id.
Crucially, sessions areCrucially, sessions are
easyeasy to implement asto implement as PHPPHP
does all the work!does all the work!
Starting or Resuming aStarting or Resuming a
SessionSession
session_start();
PHP does all the work: It looks for a valid session id in
the $_COOKIE or $_GET superglobals – if found it
initializes the data. If none found, a new session id is
created. Note that like setcookie(), this function
must be called before any echoed output to
browser.
Starting or Resuming aStarting or Resuming a
SessionSession
session_start();
When doing anything with sessions, this is always
called first!
Storing Session DataStoring Session Data
• The $_SESSION superglobal array can be used to
store any session data.
e.g.
$_SESSION[‘name’] = $name;
$_SESSION[‘age’] = $age;
Reading Session DataReading Session Data
• Data is simply read back from the $_SESSION
superglobal array.
e.g.
$name = $_SESSION[‘name’];
$age = $_SESSION[‘age’];
Session PropagationSession Propagation
• Sessions need to pass the session id between pages
as a user browses to track the session.
• It can do this in two ways:
o Cookie propagation
o URL propagation
Cookie PropagationCookie Propagation
• A cookie is stored on the users PC
containing the session id.
• It is read in whenever session_start(); is
called to initialize the session.
• Default behaviour is a cookie that expires
when the browser is closed. Cookie
properties can be modified with
session_set_cookie_params if required.
URL PropagationURL Propagation
• The session id is propagated in the URL
(…some_folder/index.php?sid=26fe536a534d3c7cde4297abb45e275a)
• PHP provides a global constant to append the
session id to any internal links, SID.
e.g.
<a href="nextpage.php?<?=SID?>">Next page</a>
Which one..?Which one..?
• The default setup of a PHP server is to use both
methods.
o it checks whether the user has cookies enabled.
o If cookies are on, PHP uses cookie propagation. If cookies are off it uses
URL propagation.
And this means..?And this means..?
• That as developers, we must be aware that sessions
can be propagated through URL, and append the
constant SID to any internal links.
• If sessions are being propagated by cookies, the
constant SID is an empty string, so the session id is
not passed twice.
Destroying a SessionDestroying a Session
Often not required, but if we want to destroy a
session:
// clear all session variables
$_SESSION = array();
// delete the session cookie if there is one
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(),'',time()-42000,'/');
}
// destroy session
session_destroy();
// avoid reusing the SID by redirecting
// back to the same page to regenerate session
header('Location: '.$_SERVER['PHP_SELF']);
Session ExpirySession Expiry
• By default, PHP sessions expire:
o after a certain length of inactivity (default 1440s),
the PHP garbage collection processes deletes
session variables. Important as most sessions will
not be explicitly destroyed.
o if propagated by cookies, default is to set a
cookie that is destroyed when the browser is
closed.
o If URL propagated, session id is lost as soon as
navigate away from the site.
Long-term SessionsLong-term Sessions
• Although it is possible to customize sessions so that
they are maintained after the browser is closed, for
most practical purposes PHP sessions can be
regarded as short-term.
• Long-term session data (e.g. ‘remember me’ boxes)
is usually maintained by explicitly setting and
retrieving cookie data.
Session Hi-jackingSession Hi-jacking
• A security issue: if a malicious user manages to
get hold of an active session id that is not their
own..
e.g.
o user 1 browsing site with cookies disabled (URL
propagation).
o user 1 logs in.
o user 1 sends an interesting link to user 2 by email.. The
URL copy and pasted contains his session id.
o user 2 looks at the link before session id is destroyed,
and ‘hijacks’ user 1’s session.
o user 2 is now logged in as user 1!!
…… rule of thumb …rule of thumb …
If you are truly security conscious you should assume
that a session propagated by URL may be
compromised. Propagation using cookies is more
secure, but still not foolproof..
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/php-classes-in-
mumbai.html

More Related Content

PPTX
PHP Cookies and Sessions
PPTX
Php sessions
PPTX
PDF
Php introduction
PPTX
PPTX
Javascript 101
PPTX
HTTP request and response
PPT
Php Presentation
PHP Cookies and Sessions
Php sessions
Php introduction
Javascript 101
HTTP request and response
Php Presentation

What's hot (20)

PDF
Operators in PHP
PPTX
Loops PHP 04
PPTX
html5.ppt
PPT
Overview of PHP and MYSQL
PPT
Advanced Cascading Style Sheets
PPTX
HTML5 audio & video
PPTX
Java script
PPTX
JavaScript Basic
PPTX
Javascript
PPT
Introduction to Javascript
PPTX
Sessions in php
PPTX
Web programming
ODP
CSS Basics
PPTX
Php Tutorial
PPTX
Introduction to HTML and CSS
PDF
Javascript basics
PDF
Introduction to php
Operators in PHP
Loops PHP 04
html5.ppt
Overview of PHP and MYSQL
Advanced Cascading Style Sheets
HTML5 audio & video
Java script
JavaScript Basic
Javascript
Introduction to Javascript
Sessions in php
Web programming
CSS Basics
Php Tutorial
Introduction to HTML and CSS
Javascript basics
Introduction to php
Ad

Viewers also liked (20)

PPSX
Sessions and cookies
PPT
Cookies and sessions
PPTX
Cookie and session
PPT
Php ssession - cookies -introduction
PPT
PHP Cookies, Sessions and Authentication
PDF
Introduction to php web programming - sessions and cookies
PPTX
Cookies PowerPoint
PDF
Deliver Files With PHP
PPTX
Php Form
PPT
Chapter 07 php forms handling
PDF
PHP Files: An Introduction
PPTX
3 php forms
ODP
Form Processing In Php
PPT
Php forms
PPT
Pakistan's mountain ranges
DOCX
Mountains In Pakistan
PPT
Plains, plateaus and deserts in pakistan
PPTX
Cookies!
PPT
Mountains of Pakistan any physiography
Sessions and cookies
Cookies and sessions
Cookie and session
Php ssession - cookies -introduction
PHP Cookies, Sessions and Authentication
Introduction to php web programming - sessions and cookies
Cookies PowerPoint
Deliver Files With PHP
Php Form
Chapter 07 php forms handling
PHP Files: An Introduction
3 php forms
Form Processing In Php
Php forms
Pakistan's mountain ranges
Mountains In Pakistan
Plains, plateaus and deserts in pakistan
Cookies!
Mountains of Pakistan any physiography
Ad

Similar to PHP - Introduction to PHP Cookies and Sessions (20)

PPT
Lecture8 php page control by okello erick
PDF
4.4 PHP Session
PPTX
Cookies and Session
ODP
Session Management & Cookies In Php
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PDF
PHP-Cookies-Sessions.pdf
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPTX
FP512 Cookies sessions
PPTX
PHP COOKIES AND SESSIONS
PPTX
PHP SESSIONS & COOKIE.pptx
PPTX
Sessions and cookies in php
PPT
Session,cookies
PPT
season management in php (WT)
PDF
Web app development_cookies_sessions_14
PPTX
lecture 13.pptx
PPTX
4 php-advanced
PPT
Cookies and sessions
PPT
PHP-07-Cookies-Sessions indepth powerpoint
PPTX
7. Sessions.pptx
Lecture8 php page control by okello erick
4.4 PHP Session
Cookies and Session
Session Management & Cookies In Php
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PHP-Cookies-Sessions.pdf
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
FP512 Cookies sessions
PHP COOKIES AND SESSIONS
PHP SESSIONS & COOKIE.pptx
Sessions and cookies in php
Session,cookies
season management in php (WT)
Web app development_cookies_sessions_14
lecture 13.pptx
4 php-advanced
Cookies and sessions
PHP-07-Cookies-Sessions indepth powerpoint
7. Sessions.pptx

More from Vibrant Technologies & Computers (20)

PPT
Buisness analyst business analysis overview ppt 5
PPT
SQL Introduction to displaying data from multiple tables
PPT
SQL- Introduction to MySQL
PPT
SQL- Introduction to SQL database
PPT
ITIL - introduction to ITIL
PPT
Salesforce - Introduction to Security & Access
PPT
Data ware housing- Introduction to olap .
PPT
Data ware housing - Introduction to data ware housing process.
PPT
Data ware housing- Introduction to data ware housing
PPT
Salesforce - classification of cloud computing
PPT
Salesforce - cloud computing fundamental
PPT
SQL- Introduction to PL/SQL
PPT
SQL- Introduction to advanced sql concepts
PPT
SQL Inteoduction to SQL manipulating of data
PPT
SQL- Introduction to SQL Set Operations
PPT
Sas - Introduction to designing the data mart
PPT
Sas - Introduction to working under change management
PPT
SAS - overview of SAS
PPT
Teradata - Architecture of Teradata
PPT
Teradata - Restoring Data
Buisness analyst business analysis overview ppt 5
SQL Introduction to displaying data from multiple tables
SQL- Introduction to MySQL
SQL- Introduction to SQL database
ITIL - introduction to ITIL
Salesforce - Introduction to Security & Access
Data ware housing- Introduction to olap .
Data ware housing - Introduction to data ware housing process.
Data ware housing- Introduction to data ware housing
Salesforce - classification of cloud computing
Salesforce - cloud computing fundamental
SQL- Introduction to PL/SQL
SQL- Introduction to advanced sql concepts
SQL Inteoduction to SQL manipulating of data
SQL- Introduction to SQL Set Operations
Sas - Introduction to designing the data mart
Sas - Introduction to working under change management
SAS - overview of SAS
Teradata - Architecture of Teradata
Teradata - Restoring Data

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPT
Teaching material agriculture food technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Unlocking AI with Model Context Protocol (MCP)
Advanced methodologies resolving dimensionality complications for autism neur...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Teaching material agriculture food technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
cuic standard and advanced reporting.pdf
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Review of recent advances in non-invasive hemoglobin estimation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

PHP - Introduction to PHP Cookies and Sessions

  • 2. Introduction to PHPIntroduction to PHP Cookies & SessionsCookies & Sessions
  • 3. So…So… Cookies Sessions Limited storage space Practically unlimited space Insecure storage client- side Reasonably securely stored server-side User controlled No user control
  • 4. How do ‘Sessions’ work?How do ‘Sessions’ work? • They are based on assigning each user a unique number, or session id. Even for extremely heavy use sites, this number can for all practical purposes can be regarded as unique. e.g. 26fe536a534d3c7cde4297abb45e275a
  • 5. How do ‘Sessions’ work?How do ‘Sessions’ work? • This session id is stored in a cookie, or passed in the URL between pages while the user browses. • The data to be stored (e.g. name, log-in state, etc.) is stored securely server-side in a PHP superglobal, and referenced using the session id.
  • 6. Crucially, sessions areCrucially, sessions are easyeasy to implement asto implement as PHPPHP does all the work!does all the work!
  • 7. Starting or Resuming aStarting or Resuming a SessionSession session_start(); PHP does all the work: It looks for a valid session id in the $_COOKIE or $_GET superglobals – if found it initializes the data. If none found, a new session id is created. Note that like setcookie(), this function must be called before any echoed output to browser.
  • 8. Starting or Resuming aStarting or Resuming a SessionSession session_start(); When doing anything with sessions, this is always called first!
  • 9. Storing Session DataStoring Session Data • The $_SESSION superglobal array can be used to store any session data. e.g. $_SESSION[‘name’] = $name; $_SESSION[‘age’] = $age;
  • 10. Reading Session DataReading Session Data • Data is simply read back from the $_SESSION superglobal array. e.g. $name = $_SESSION[‘name’]; $age = $_SESSION[‘age’];
  • 11. Session PropagationSession Propagation • Sessions need to pass the session id between pages as a user browses to track the session. • It can do this in two ways: o Cookie propagation o URL propagation
  • 12. Cookie PropagationCookie Propagation • A cookie is stored on the users PC containing the session id. • It is read in whenever session_start(); is called to initialize the session. • Default behaviour is a cookie that expires when the browser is closed. Cookie properties can be modified with session_set_cookie_params if required.
  • 13. URL PropagationURL Propagation • The session id is propagated in the URL (…some_folder/index.php?sid=26fe536a534d3c7cde4297abb45e275a) • PHP provides a global constant to append the session id to any internal links, SID. e.g. <a href="nextpage.php?<?=SID?>">Next page</a>
  • 14. Which one..?Which one..? • The default setup of a PHP server is to use both methods. o it checks whether the user has cookies enabled. o If cookies are on, PHP uses cookie propagation. If cookies are off it uses URL propagation.
  • 15. And this means..?And this means..? • That as developers, we must be aware that sessions can be propagated through URL, and append the constant SID to any internal links. • If sessions are being propagated by cookies, the constant SID is an empty string, so the session id is not passed twice.
  • 16. Destroying a SessionDestroying a Session Often not required, but if we want to destroy a session: // clear all session variables $_SESSION = array(); // delete the session cookie if there is one if (isset($_COOKIE[session_name()])) { setcookie(session_name(),'',time()-42000,'/'); } // destroy session session_destroy(); // avoid reusing the SID by redirecting // back to the same page to regenerate session header('Location: '.$_SERVER['PHP_SELF']);
  • 17. Session ExpirySession Expiry • By default, PHP sessions expire: o after a certain length of inactivity (default 1440s), the PHP garbage collection processes deletes session variables. Important as most sessions will not be explicitly destroyed. o if propagated by cookies, default is to set a cookie that is destroyed when the browser is closed. o If URL propagated, session id is lost as soon as navigate away from the site.
  • 18. Long-term SessionsLong-term Sessions • Although it is possible to customize sessions so that they are maintained after the browser is closed, for most practical purposes PHP sessions can be regarded as short-term. • Long-term session data (e.g. ‘remember me’ boxes) is usually maintained by explicitly setting and retrieving cookie data.
  • 19. Session Hi-jackingSession Hi-jacking • A security issue: if a malicious user manages to get hold of an active session id that is not their own.. e.g. o user 1 browsing site with cookies disabled (URL propagation). o user 1 logs in. o user 1 sends an interesting link to user 2 by email.. The URL copy and pasted contains his session id. o user 2 looks at the link before session id is destroyed, and ‘hijacks’ user 1’s session. o user 2 is now logged in as user 1!!
  • 20. …… rule of thumb …rule of thumb … If you are truly security conscious you should assume that a session propagated by URL may be compromised. Propagation using cookies is more secure, but still not foolproof..
  • 21. ThankThank You !!!You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/php-classes-in- mumbai.html