SlideShare a Scribd company logo
Introduction to PHP
Web programming
Week 11, day2
Cookies
Cookies
• HTTP is a stateless protocol; this means that the web server does not know
(or care) whether two requests comes from the same user or not; it just
handles each request without regard to the context in which it happens.
• Cookies are used to maintain the state in between requests—even when
they occur at large time intervals from each other.
• Cookies allow your applications to store a small amount of textual data
(typically,4-6kB) on a Web client browser.
• There are a number of possible uses for cookies, although their most
common one is maintaining state of a user
Creating cookie
• setcookie(‚userid", "100", time() + 86400);
• This simply sets a cookie variable named ‚userid‛ with value ‚100‛
and this variable value will be available till next 86400 seconds
from current time
Cookie variable name
variable value
Expiration time.
Accessing Cookies
• echo $_COOKIE*’userid’+; // prints 100
• Cookie as array
– setcookie("test_cookie[0]", "foo");
– setcookie("test_cookie[1]", "bar");
– setcookie("test_cookie[2]", "bar");
• var_dump($_COOKIE*‘test_cookie’+);
Destroying Cookies
• There is no special methods to destroy a cookie, We achieve it by
setting the cookie time into a past time so that it destroys it
–Eg : setcookie(‘userid’,100,time()-100);
Sessions
Sessions
• Session serve the same purpose of cookies that is sessions are used
to maintain the state in between requests
• Session can be started in two ways in PHP
1. By changing the session.auto_start configuration setting in php.ini
2. Calling session_start() on the beginning of each pages wherever you
use session(Most common way)
Note: session_start() must be called before any output is sent to the browser
Creating and accessing session
• Once session is started you can create and access session variables
like any other arrays in PHP
– $_SESSION[‘userid’+ = 100;
– echo $_SESSION*‘userid’+; //prints 100
Session variable name variable value
Destroying session
• There are two methods to destroy a session variable
1. Using unset() function
• Eg unsset($_SESSION*‘userid’+)
2. Calling session_destroy() method. This will effectively destroy all the
session variables. So for deleting only one variable you should go for
the previous method
• Session_destroy()
Comparison
 cookies are stored in the user's
browser
 A cookie can keep information in the
user's browser until deleted by user or set
as per the timer. It will not be destroyed
even if you close the browser.
 Cookies can only store string
 we can save cookie for future reference
 Sessions are stored in server
 A session is available as long as the
browser is opened. User cant disable the
session. It will be destroyed if you close the
browser
 Can store not only strings but also
objects
 session cant be.
Cookies Session
HTTP Headers
Headers - Redirection
• The most common use of headers is to redirect the user to another
page. To do this,we use the Location header:
Eg: header("Location: http://phparch.com");
• header() must be called before any other output, including any
whitespace characters outside of PHP tags, as well as all HTML
data.
• If you fail to abide by this rule, two things will happen: your
header will have no effect, and PHP may output an error.
Questions?
‚A good question deserve a good grade…‛
End of day
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

PPTX
Introduction to php
PDF
Php array
PPT
Php Presentation
PPT
Web Development using HTML & CSS
PDF
JavaScript - Chapter 12 - Document Object Model
PPTX
PHP Cookies and Sessions
PPTX
HTTP request and response
PPTX
Introduction to php
Introduction to php
Php array
Php Presentation
Web Development using HTML & CSS
JavaScript - Chapter 12 - Document Object Model
PHP Cookies and Sessions
HTTP request and response
Introduction to php

What's hot (20)

PPT
Php Lecture Notes
PPT
DOCX
PHP NOTES FOR BEGGINERS
PPTX
Session tracking in servlets
PPT
Oops in PHP
PPT
PPT
Jsp ppt
PPT
Js ppt
PDF
jQuery for beginners
PPT
01 Php Introduction
PPT
Web servers
PPTX
Web programming
PPTX
Css animation
PPTX
An Overview of HTML, CSS & Java Script
PPT
Node.js Basics
PDF
JavaScript Programming
PPT
Jdbc ppt
PPTX
Introduction to Angularjs
PPTX
Cascading Style Sheet (CSS)
Php Lecture Notes
PHP NOTES FOR BEGGINERS
Session tracking in servlets
Oops in PHP
Jsp ppt
Js ppt
jQuery for beginners
01 Php Introduction
Web servers
Web programming
Css animation
An Overview of HTML, CSS & Java Script
Node.js Basics
JavaScript Programming
Jdbc ppt
Introduction to Angularjs
Cascading Style Sheet (CSS)
Ad

Viewers also liked (20)

PDF
Introduction to php web programming - get and post
PDF
Introduction to PHP
PPT
Class 6 - PHP Web Programming
PPT
PHP - Introduction to PHP Cookies and Sessions
PPSX
Sessions and cookies
ODP
PHP Web Programming
PPT
ODP
Coding In Php
PPT
Php Coding Convention
PPTX
PHP FUNCTIONS
PDF
開始網站設計
PDF
MySQL資料庫設計規劃與phpMyAdmin
PDF
PHP an intro -1
PPT
Php ssession - cookies -introduction
PPSX
PHP Comprehensive Overview
PDF
Php course-syllabus
PDF
AI Chatbot Service Framework based on Backpropagation Network for Predicting ...
PPT
Pakistan's mountain ranges
PPT
PHP Cookies, Sessions and Authentication
Introduction to php web programming - get and post
Introduction to PHP
Class 6 - PHP Web Programming
PHP - Introduction to PHP Cookies and Sessions
Sessions and cookies
PHP Web Programming
Coding In Php
Php Coding Convention
PHP FUNCTIONS
開始網站設計
MySQL資料庫設計規劃與phpMyAdmin
PHP an intro -1
Php ssession - cookies -introduction
PHP Comprehensive Overview
Php course-syllabus
AI Chatbot Service Framework based on Backpropagation Network for Predicting ...
Pakistan's mountain ranges
PHP Cookies, Sessions and Authentication
Ad

Similar to Introduction to php web programming - sessions and cookies (20)

PPT
Lecture8 php page control by okello erick
PPTX
PHP COOKIES AND SESSIONS
PPTX
PHP SESSIONS & COOKIE.pptx
PPTX
FP512 Cookies sessions
PDF
4.4 PHP Session
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PDF
Web app development_cookies_sessions_14
PPT
Session,cookies
PDF
PHP-Cookies-Sessions.pdf
PPTX
Cookie and session
ODP
Session Management & Cookies In Php
PPTX
Session and cookies ,get and post methods
PPT
Manish
PPTX
Cookies and Session
PPTX
4 php-advanced
PPTX
Session and Cookies
PPTX
lecture 13.pptx
PPTX
Sessions in php
Lecture8 php page control by okello erick
PHP COOKIES AND SESSIONS
PHP SESSIONS & COOKIE.pptx
FP512 Cookies sessions
4.4 PHP Session
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Web app development_cookies_sessions_14
Session,cookies
PHP-Cookies-Sessions.pdf
Cookie and session
Session Management & Cookies In Php
Session and cookies ,get and post methods
Manish
Cookies and Session
4 php-advanced
Session and Cookies
lecture 13.pptx
Sessions in php

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PPTX
Cloud computing and distributed systems.
PDF
Approach and Philosophy of On baking technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
KodekX | Application Modernization Development
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Electronic commerce courselecture one. Pdf
Cloud computing and distributed systems.
Approach and Philosophy of On baking technology
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
KodekX | Application Modernization Development
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
sap open course for s4hana steps from ECC to s4
Network Security Unit 5.pdf for BCA BBA.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
MIND Revenue Release Quarter 2 2025 Press Release

Introduction to php web programming - sessions and cookies

  • 1. Introduction to PHP Web programming Week 11, day2
  • 3. Cookies • HTTP is a stateless protocol; this means that the web server does not know (or care) whether two requests comes from the same user or not; it just handles each request without regard to the context in which it happens. • Cookies are used to maintain the state in between requests—even when they occur at large time intervals from each other. • Cookies allow your applications to store a small amount of textual data (typically,4-6kB) on a Web client browser. • There are a number of possible uses for cookies, although their most common one is maintaining state of a user
  • 4. Creating cookie • setcookie(‚userid", "100", time() + 86400); • This simply sets a cookie variable named ‚userid‛ with value ‚100‛ and this variable value will be available till next 86400 seconds from current time Cookie variable name variable value Expiration time.
  • 5. Accessing Cookies • echo $_COOKIE*’userid’+; // prints 100 • Cookie as array – setcookie("test_cookie[0]", "foo"); – setcookie("test_cookie[1]", "bar"); – setcookie("test_cookie[2]", "bar"); • var_dump($_COOKIE*‘test_cookie’+);
  • 6. Destroying Cookies • There is no special methods to destroy a cookie, We achieve it by setting the cookie time into a past time so that it destroys it –Eg : setcookie(‘userid’,100,time()-100);
  • 8. Sessions • Session serve the same purpose of cookies that is sessions are used to maintain the state in between requests • Session can be started in two ways in PHP 1. By changing the session.auto_start configuration setting in php.ini 2. Calling session_start() on the beginning of each pages wherever you use session(Most common way) Note: session_start() must be called before any output is sent to the browser
  • 9. Creating and accessing session • Once session is started you can create and access session variables like any other arrays in PHP – $_SESSION[‘userid’+ = 100; – echo $_SESSION*‘userid’+; //prints 100 Session variable name variable value
  • 10. Destroying session • There are two methods to destroy a session variable 1. Using unset() function • Eg unsset($_SESSION*‘userid’+) 2. Calling session_destroy() method. This will effectively destroy all the session variables. So for deleting only one variable you should go for the previous method • Session_destroy()
  • 11. Comparison  cookies are stored in the user's browser  A cookie can keep information in the user's browser until deleted by user or set as per the timer. It will not be destroyed even if you close the browser.  Cookies can only store string  we can save cookie for future reference  Sessions are stored in server  A session is available as long as the browser is opened. User cant disable the session. It will be destroyed if you close the browser  Can store not only strings but also objects  session cant be. Cookies Session
  • 13. Headers - Redirection • The most common use of headers is to redirect the user to another page. To do this,we use the Location header: Eg: header("Location: http://phparch.com"); • header() must be called before any other output, including any whitespace characters outside of PHP tags, as well as all HTML data. • If you fail to abide by this rule, two things will happen: your header will have no effect, and PHP may output an error.
  • 14. Questions? ‚A good question deserve a good grade…‛
  • 16. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 17. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com