SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Php with mysql ppt
Php with mysql pptPhp with mysql ppt
Php with mysql ppt
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Uploading a file with php
Uploading a file with phpUploading a file with php
Uploading a file with php
 
Php functions
Php functionsPhp functions
Php functions
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Php
PhpPhp
Php
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Cookies & Session
Cookies & SessionCookies & Session
Cookies & Session
 
Php array
Php arrayPhp array
Php array
 
PHP
PHPPHP
PHP
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 

Andere mochten auch

Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introductionProgrammer Blog
 
PHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and AuthenticationPHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and AuthenticationGerard Sychay
 
Cookies PowerPoint
Cookies PowerPointCookies PowerPoint
Cookies PowerPointemurfield
 
Deliver Files With PHP
Deliver Files With PHPDeliver Files With PHP
Deliver Files With PHPThomas Weinert
 
Php Form
Php FormPhp Form
Php Formlotlot
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handlingDhani Ahmad
 
PHP Files: An Introduction
PHP Files: An IntroductionPHP Files: An Introduction
PHP Files: An IntroductionJacques Woodcock
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In PhpHarit Kothari
 
Pakistan's mountain ranges
Pakistan's mountain rangesPakistan's mountain ranges
Pakistan's mountain rangestehseen bukhari
 
Plains, plateaus and deserts in pakistan
Plains, plateaus and deserts in pakistanPlains, plateaus and deserts in pakistan
Plains, plateaus and deserts in pakistanAqsa Manzoor
 
Mountains of Pakistan any physiography
Mountains of Pakistan any physiography Mountains of Pakistan any physiography
Mountains of Pakistan any physiography GCUF
 
Physical features of pakistan
Physical features of pakistanPhysical features of pakistan
Physical features of pakistanAmad Qurashi
 

Andere mochten auch (20)

Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
PHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and AuthenticationPHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and Authentication
 
Introduction to php web programming - sessions and cookies
Introduction to php   web programming - sessions and cookiesIntroduction to php   web programming - sessions and cookies
Introduction to php web programming - sessions and cookies
 
Cookies PowerPoint
Cookies PowerPointCookies PowerPoint
Cookies PowerPoint
 
Deliver Files With PHP
Deliver Files With PHPDeliver Files With PHP
Deliver Files With PHP
 
Php Form
Php FormPhp Form
Php Form
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handling
 
PHP Files: An Introduction
PHP Files: An IntroductionPHP Files: An Introduction
PHP Files: An Introduction
 
3 php forms
3 php forms3 php forms
3 php forms
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Php forms
Php formsPhp forms
Php forms
 
Pakistan's mountain ranges
Pakistan's mountain rangesPakistan's mountain ranges
Pakistan's mountain ranges
 
Mountains In Pakistan
Mountains In PakistanMountains In Pakistan
Mountains In Pakistan
 
Plains, plateaus and deserts in pakistan
Plains, plateaus and deserts in pakistanPlains, plateaus and deserts in pakistan
Plains, plateaus and deserts in pakistan
 
Cookies!
Cookies!Cookies!
Cookies!
 
Mountains of Pakistan any physiography
Mountains of Pakistan any physiography Mountains of Pakistan any physiography
Mountains of Pakistan any physiography
 
Physical features of pakistan
Physical features of pakistanPhysical features of pakistan
Physical features of pakistan
 
Geography of Pakistan
Geography of PakistanGeography of Pakistan
Geography of Pakistan
 

Ähnlich wie PHP Sessions Explained: How They Work, Starting, Storing and Destroying Data

Php 07-cookies-sessions
Php 07-cookies-sessionsPhp 07-cookies-sessions
Php 07-cookies-sessionsYUSRA FERNANDO
 
season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)kunjan shah
 
PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSDegu8
 
Session and Cookies.pdf
Session and Cookies.pdfSession and Cookies.pdf
Session and Cookies.pdfHamnaGhani1
 
Module-5_WTA_Managing State & jQuery
Module-5_WTA_Managing State & jQueryModule-5_WTA_Managing State & jQuery
Module-5_WTA_Managing State & jQuerySIVAKUMAR V
 
7. Sessions.pptx
7. Sessions.pptx7. Sessions.pptx
7. Sessions.pptxmawordTz
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptSreejithVP7
 
Cookies and Session
Cookies and SessionCookies and Session
Cookies and SessionKoraStats
 
Maximize your Cache for No Cash
Maximize your Cache for No CashMaximize your Cache for No Cash
Maximize your Cache for No CashYorick Phoenix
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erickokelloerick
 
Ruby on Rails Security Guide
Ruby on Rails Security GuideRuby on Rails Security Guide
Ruby on Rails Security Guideihji
 
What should a hacker know about WebDav?
What should a hacker know about WebDav?What should a hacker know about WebDav?
What should a hacker know about WebDav?Mikhail Egorov
 
Session,Cookies and Authentication
Session,Cookies and AuthenticationSession,Cookies and Authentication
Session,Cookies and AuthenticationKnoldus Inc.
 
The Hacker's Guide To Session Hijacking
The Hacker's Guide To Session HijackingThe Hacker's Guide To Session Hijacking
The Hacker's Guide To Session HijackingPatrycja Wegrzynowicz
 
Joomla! security jday2015
Joomla! security jday2015Joomla! security jday2015
Joomla! security jday2015kriptonium
 
Enterprise java unit-2_chapter-2
Enterprise  java unit-2_chapter-2Enterprise  java unit-2_chapter-2
Enterprise java unit-2_chapter-2sandeep54552
 

Ähnlich wie PHP Sessions Explained: How They Work, Starting, Storing and Destroying Data (20)

Php 07-cookies-sessions
Php 07-cookies-sessionsPhp 07-cookies-sessions
Php 07-cookies-sessions
 
season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)
 
PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONS
 
Session and Cookies.pdf
Session and Cookies.pdfSession and Cookies.pdf
Session and Cookies.pdf
 
Module-5_WTA_Managing State & jQuery
Module-5_WTA_Managing State & jQueryModule-5_WTA_Managing State & jQuery
Module-5_WTA_Managing State & jQuery
 
7. Sessions.pptx
7. Sessions.pptx7. Sessions.pptx
7. Sessions.pptx
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
 
Sessions&cookies
Sessions&cookiesSessions&cookies
Sessions&cookies
 
Security in php
Security in phpSecurity in php
Security in php
 
Cookies and Session
Cookies and SessionCookies and Session
Cookies and Session
 
Session and cookies,get and post methods
Session and cookies,get and post methodsSession and cookies,get and post methods
Session and cookies,get and post methods
 
Maximize your Cache for No Cash
Maximize your Cache for No CashMaximize your Cache for No Cash
Maximize your Cache for No Cash
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erick
 
Ruby on Rails Security Guide
Ruby on Rails Security GuideRuby on Rails Security Guide
Ruby on Rails Security Guide
 
What should a hacker know about WebDav?
What should a hacker know about WebDav?What should a hacker know about WebDav?
What should a hacker know about WebDav?
 
Session,Cookies and Authentication
Session,Cookies and AuthenticationSession,Cookies and Authentication
Session,Cookies and Authentication
 
The Hacker's Guide To Session Hijacking
The Hacker's Guide To Session HijackingThe Hacker's Guide To Session Hijacking
The Hacker's Guide To Session Hijacking
 
Php sessions
Php sessionsPhp sessions
Php sessions
 
Joomla! security jday2015
Joomla! security jday2015Joomla! security jday2015
Joomla! security jday2015
 
Enterprise java unit-2_chapter-2
Enterprise  java unit-2_chapter-2Enterprise  java unit-2_chapter-2
Enterprise java unit-2_chapter-2
 

Mehr von Vibrant Technologies & Computers

Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.Vibrant Technologies & Computers
 

Mehr von Vibrant Technologies & Computers (20)

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

Kürzlich hochgeladen

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Kürzlich hochgeladen (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

PHP Sessions Explained: How They Work, Starting, Storing and Destroying Data

  • 1.
  • 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