SlideShare a Scribd company logo
Cookies and Sessions in PHP
Why Cookies and Sessions are Used?

   HTTP is a stateless protocol. This means that each request is handled
    independently of all the other requests and it means that a server or a
    script cannot remember if a user has been there before.
   However, knowing if a user has been there before is often required and
    therefore something known as cookies and sessions have been
    implemented.
What is a Cookie?

   A cookie is a piece of text that a Web server can store on a user's hard
    disk.
   A cookie is a variable, sent by the server to the browser.
   Cookies allow a Web site to store information on a user's machine and
    later retrieve it. The pieces of information are stored as name-value pairs.
What is a Cookie?
   Each cookie on the user’s computer is connected to a particular domain.
   Each time the same computer requests a page with a browser, it will send
    the cookie too.
   Each cookie can store up to 4kB of data.
   A maximum of 20 cookies can be stored on a user’s PC per domain.
When are Cookies Created?
   When a new webpage is loaded - for example after a 'submit' button is
    pressed the data handling page would be responsible for storing the
    values in a cookie.
   If the user has elected to disable cookies then the write operation will
    fail, and subsequent sites which rely on the cookie will either have to
    take a default action.
Example (1)
1.   User sends a request for page at www.example.com for the first time.




                                page request
Example (2)
2.   Server sends back the page html to the browser AND stores some data in
      a cookie on the user’s PC.




                           html


                           cookie data
Example (1)
3.   At the next page request for domain www.example.com, all cookie data
      associated with this domain is sent too.




                               page request


                                 cookie data
What's in a Cookie?
   Each cookie is effectively a small lookup table containing pairs of (key,
    data) values - for example (firstname, John) (lastname,Peter).
    Once the cookie has been read by the code on the server or client
    computer, the data can be retrieved and used to customise the web page
    appropriately.
Set a cookie
setcookie(name [,value [,expire [,path [,domain [,secure]]]]])

name = cookie name
value = data to store (string)
expire = UNIX timestamp when the cookie expires. Default is that cookie
   expires when browser is closed.
path = Path on the server within and below which the cookie is available on.
domain = Domain at which the cookie is available for.
secure = If cookie should be sent over HTTPS connection only. Default false.
Set a cookie - examples
setcookie(‘name’,’Robert’)

  This command will set the cookie called name on the user’s PC containing
  the data Robert. It will be available to all pages in the same directory or
  subdirectory of the page that set it (the default path and domain). It will
  expire and be deleted when the browser is closed (default expire).
Set a cookie - examples
setcookie(‘age’,’20’,time()+60*60*24*30)


  This command will set the cookie called age on the user’s PC containing
  the data 20. It will be available to all pages in the same directory or
  subdirectory of the page that set it (the default path and domain). It will
  expire and be deleted after 30 days.
Set a cookie - examples
setcookie(‘gender’,’male’,0,’/’)


   This command will set the cookie called gender on the user’s PC containing
   the data male. It will be available within the entire domain that set it. It
   will expire and be deleted when the browser is closed.
Read cookie data

   All cookie data is available through the superglobal
    $_COOKIE:
    $variable = $_COOKIE[‘cookie_name’]

    or
    $variable = $HTTP_COOKIE_VARS[‘cookie_name’];

    e.g.
    $age = $_COOKIE[‘age’]
Delete a cookie
   To remove a cookie, simply overwrite the cookie with a new one with an
    expiry time in the past…

    setcookie(‘cookie_name’,’’,time()-6000)

   Note that theoretically any number taken away from the time() function
    should do, but due to variations in local computer times, it is advisable to
    use a day or two.
Problems with Cookies
   Browsers can refuse to accept cookies.
   Additionally, it adds network overhead to
    send lots of information back and forth.
   There are also limits to the amount of
    information that can be sent
   Some information you just don’t want to save on the client’s computer.
Sessions

   A Session allows 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.
   Session variables hold information about one single user, and are available
    to all pages in one application.
How Session Works?
   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.
   The UID is either stored in a cookie or is propagated in the URL.
Starting a PHP Session

   Before you can store user information in your PHP
    session, you must first start up the session.
   The session_start() function must appear BEFORE
    the <html> tag.
   <?php session_start(); ?>
    <html>
    <body>

    </body>
    </html>
Storing a Session Variable
   The correct way to store and retrieve session variables is to use the PHP
    $_SESSION variable.
   <?php
    session_start();
    // store session data
    $_SESSION['views']=1;
    ?>
    <html>
    <body

    </body>
    </html>
Retrieving a Session Variable

   <html>
    <body>

    <?php
    //retrieve session data
    echo "Pageviews=". $_SESSION['views'];
    ?>
    </body>
    </html>
   Display:
               Pageviews = 1
Destroying a Session
   The unset() function is used to free the specified session variable.
   <?php
    unset($_SESSION['views']);
    ?>
   You can also completely destroy the session by calling the
    session_destroy() function:
   <?php
    session_destroy();
    ?>
   session_destroy() will reset your session and you will lose all your stored
    session data.
Cookies vs. Sessions

               Cookies                                Sessions

                                           Sessions are stored on server
   Cookies are stored on client side       side
   Cookies can only store strings.        Sessions can store objects.
   Cookies can be set to a long           When users close their browser,
    lifespan.                               they also lost the session.
Thanks!

More Related Content

What's hot

PHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP ServerPHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP Server
Rajiv Bhatia
 
Internet Cookies
Internet CookiesInternet Cookies
Internet Cookies
anita gouda
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Bootstrap
BootstrapBootstrap
Bootstrap
Jadson Santos
 
Javascript
JavascriptJavascript
Javascript
Manav Prasad
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Css selectors
Css selectorsCss selectors
Css selectors
Parth Trivedi
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
Dominic Arrojado
 
Advanced Web Development
Advanced Web DevelopmentAdvanced Web Development
Advanced Web Development
Robert J. Stein
 
Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NETShingalaKrupa
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
Ravinder Kamboj
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginners
Singsys Pte Ltd
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
Nisa Soomro
 
Master pages
Master pagesMaster pages
Master pages
teach4uin
 
PHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and SessionsPHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and Sessions
Vibrant Technologies & Computers
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 

What's hot (20)

PHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP ServerPHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP Server
 
Internet Cookies
Internet CookiesInternet Cookies
Internet Cookies
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Ajax
AjaxAjax
Ajax
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Css selectors
Css selectorsCss selectors
Css selectors
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Advanced Web Development
Advanced Web DevelopmentAdvanced Web Development
Advanced Web Development
 
Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NET
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginners
 
Servlets
ServletsServlets
Servlets
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Master pages
Master pagesMaster pages
Master pages
 
PHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and SessionsPHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and Sessions
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 

Viewers also liked

Web Cookies
Web CookiesWeb Cookies
Web Cookiesapwebco
 
Presentation on Internet Cookies
Presentation on Internet CookiesPresentation on Internet Cookies
Presentation on Internet Cookies
Ritika Barethia
 
Hypertext, hypermedia and multimedia
Hypertext, hypermedia and multimediaHypertext, hypermedia and multimedia
Hypertext, hypermedia and multimedia
fernandadavalos2566
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
Cookies PowerPoint
Cookies PowerPointCookies PowerPoint
Cookies PowerPointemurfield
 

Viewers also liked (7)

Web Cookies
Web CookiesWeb Cookies
Web Cookies
 
Hypertext
HypertextHypertext
Hypertext
 
Presentation on Internet Cookies
Presentation on Internet CookiesPresentation on Internet Cookies
Presentation on Internet Cookies
 
Hypertext, hypermedia and multimedia
Hypertext, hypermedia and multimediaHypertext, hypermedia and multimedia
Hypertext, hypermedia and multimedia
 
Javascript
JavascriptJavascript
Javascript
 
Cookies PowerPoint
Cookies PowerPointCookies PowerPoint
Cookies PowerPoint
 
Cookies!
Cookies!Cookies!
Cookies!
 

Similar to Sessions and cookies

PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
Programmer Blog
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Hassen Poreya
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
Jalpesh Vasa
 
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
okelloerick
 
javaScriptCookies.pptx
javaScriptCookies.pptxjavaScriptCookies.pptx
javaScriptCookies.pptx
MattMarino13
 
19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx
ssuser4a97d3
 
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
SreejithVP7
 
PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONS
Degu8
 
Ch4(saving state with cookies and query strings)
Ch4(saving state with cookies and query strings)Ch4(saving state with cookies and query strings)
Ch4(saving state with cookies and query strings)
Chhom Karath
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessionssalissal
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptx
ITNet
 
Cookies-PHP
Cookies-PHPCookies-PHP
Cookies-PHP
priyavanim
 
Cookies and Session
Cookies and SessionCookies and Session
Cookies and Session
KoraStats
 

Similar to Sessions and cookies (20)

PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
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
 
javaScriptCookies.pptx
javaScriptCookies.pptxjavaScriptCookies.pptx
javaScriptCookies.pptx
 
19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .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
 
PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONS
 
Session,cookies
Session,cookiesSession,cookies
Session,cookies
 
Cookies
CookiesCookies
Cookies
 
ASP.NET-Web Programming - Sessions and Cookies
ASP.NET-Web Programming - Sessions and CookiesASP.NET-Web Programming - Sessions and Cookies
ASP.NET-Web Programming - Sessions and Cookies
 
Manish
ManishManish
Manish
 
Ch4(saving state with cookies and query strings)
Ch4(saving state with cookies and query strings)Ch4(saving state with cookies and query strings)
Ch4(saving state with cookies and query strings)
 
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 and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Sessions n cookies
Sessions n cookiesSessions n cookies
Sessions n cookies
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptx
 
Cookies-PHP
Cookies-PHPCookies-PHP
Cookies-PHP
 
Cookies and Session
Cookies and SessionCookies and Session
Cookies and Session
 

More from www.netgains.org

Exploring iTools
Exploring iToolsExploring iTools
Exploring iTools
www.netgains.org
 
What is a Responsive Website
What is a Responsive WebsiteWhat is a Responsive Website
What is a Responsive Website
www.netgains.org
 
Twitter bootstrap1
Twitter bootstrap1Twitter bootstrap1
Twitter bootstrap1
www.netgains.org
 
Magento
MagentoMagento
Dream weaver
Dream weaverDream weaver
Dream weaver
www.netgains.org
 
Introduction to wordpress & theme implementation
Introduction to wordpress & theme implementationIntroduction to wordpress & theme implementation
Introduction to wordpress & theme implementationwww.netgains.org
 
Web application security
Web application securityWeb application security
Web application security
www.netgains.org
 
Introduction to Html5
Introduction to Html5Introduction to Html5
Introduction to Html5
www.netgains.org
 

More from www.netgains.org (8)

Exploring iTools
Exploring iToolsExploring iTools
Exploring iTools
 
What is a Responsive Website
What is a Responsive WebsiteWhat is a Responsive Website
What is a Responsive Website
 
Twitter bootstrap1
Twitter bootstrap1Twitter bootstrap1
Twitter bootstrap1
 
Magento
MagentoMagento
Magento
 
Dream weaver
Dream weaverDream weaver
Dream weaver
 
Introduction to wordpress & theme implementation
Introduction to wordpress & theme implementationIntroduction to wordpress & theme implementation
Introduction to wordpress & theme implementation
 
Web application security
Web application securityWeb application security
Web application security
 
Introduction to Html5
Introduction to Html5Introduction to Html5
Introduction to Html5
 

Recently uploaded

Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 

Recently uploaded (20)

Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 

Sessions and cookies

  • 2. Why Cookies and Sessions are Used?  HTTP is a stateless protocol. This means that each request is handled independently of all the other requests and it means that a server or a script cannot remember if a user has been there before.  However, knowing if a user has been there before is often required and therefore something known as cookies and sessions have been implemented.
  • 3. What is a Cookie?  A cookie is a piece of text that a Web server can store on a user's hard disk.  A cookie is a variable, sent by the server to the browser.  Cookies allow a Web site to store information on a user's machine and later retrieve it. The pieces of information are stored as name-value pairs.
  • 4. What is a Cookie?  Each cookie on the user’s computer is connected to a particular domain.  Each time the same computer requests a page with a browser, it will send the cookie too.  Each cookie can store up to 4kB of data.  A maximum of 20 cookies can be stored on a user’s PC per domain.
  • 5. When are Cookies Created?  When a new webpage is loaded - for example after a 'submit' button is pressed the data handling page would be responsible for storing the values in a cookie.  If the user has elected to disable cookies then the write operation will fail, and subsequent sites which rely on the cookie will either have to take a default action.
  • 6. Example (1) 1. User sends a request for page at www.example.com for the first time. page request
  • 7. Example (2) 2. Server sends back the page html to the browser AND stores some data in a cookie on the user’s PC. html cookie data
  • 8. Example (1) 3. At the next page request for domain www.example.com, all cookie data associated with this domain is sent too. page request cookie data
  • 9. What's in a Cookie?  Each cookie is effectively a small lookup table containing pairs of (key, data) values - for example (firstname, John) (lastname,Peter).  Once the cookie has been read by the code on the server or client computer, the data can be retrieved and used to customise the web page appropriately.
  • 10. Set a cookie setcookie(name [,value [,expire [,path [,domain [,secure]]]]]) name = cookie name value = data to store (string) expire = UNIX timestamp when the cookie expires. Default is that cookie expires when browser is closed. path = Path on the server within and below which the cookie is available on. domain = Domain at which the cookie is available for. secure = If cookie should be sent over HTTPS connection only. Default false.
  • 11. Set a cookie - examples setcookie(‘name’,’Robert’) This command will set the cookie called name on the user’s PC containing the data Robert. It will be available to all pages in the same directory or subdirectory of the page that set it (the default path and domain). It will expire and be deleted when the browser is closed (default expire).
  • 12. Set a cookie - examples setcookie(‘age’,’20’,time()+60*60*24*30) This command will set the cookie called age on the user’s PC containing the data 20. It will be available to all pages in the same directory or subdirectory of the page that set it (the default path and domain). It will expire and be deleted after 30 days.
  • 13. Set a cookie - examples setcookie(‘gender’,’male’,0,’/’) This command will set the cookie called gender on the user’s PC containing the data male. It will be available within the entire domain that set it. It will expire and be deleted when the browser is closed.
  • 14. Read cookie data  All cookie data is available through the superglobal $_COOKIE: $variable = $_COOKIE[‘cookie_name’] or $variable = $HTTP_COOKIE_VARS[‘cookie_name’]; e.g. $age = $_COOKIE[‘age’]
  • 15. Delete a cookie  To remove a cookie, simply overwrite the cookie with a new one with an expiry time in the past… setcookie(‘cookie_name’,’’,time()-6000)  Note that theoretically any number taken away from the time() function should do, but due to variations in local computer times, it is advisable to use a day or two.
  • 16. Problems with Cookies  Browsers can refuse to accept cookies.  Additionally, it adds network overhead to send lots of information back and forth.  There are also limits to the amount of information that can be sent  Some information you just don’t want to save on the client’s computer.
  • 17. Sessions  A Session allows 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.  Session variables hold information about one single user, and are available to all pages in one application.
  • 18. How Session Works?  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.  The UID is either stored in a cookie or is propagated in the URL.
  • 19. Starting a PHP Session  Before you can store user information in your PHP session, you must first start up the session.  The session_start() function must appear BEFORE the <html> tag.  <?php session_start(); ?> <html> <body> </body> </html>
  • 20. Storing a Session Variable  The correct way to store and retrieve session variables is to use the PHP $_SESSION variable.  <?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body </body> </html>
  • 21. Retrieving a Session Variable  <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html>  Display: Pageviews = 1
  • 22. Destroying a Session  The unset() function is used to free the specified session variable.  <?php unset($_SESSION['views']); ?>  You can also completely destroy the session by calling the session_destroy() function:  <?php session_destroy(); ?>  session_destroy() will reset your session and you will lose all your stored session data.
  • 23. Cookies vs. Sessions Cookies Sessions  Sessions are stored on server  Cookies are stored on client side side  Cookies can only store strings.  Sessions can store objects.  Cookies can be set to a long  When users close their browser, lifespan. they also lost the session.