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

What's hot (20)

PDF
3. Java Script
Jalpesh Vasa
 
PPTX
Cookies and sessions
Sukrit Gupta
 
PPT
Javascript
mussawir20
 
PPT
Java Script ppt
Priya Goyal
 
PPT
Cookies and sessions
Lena Petsenchuk
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PPT
MySql slides (ppt)
webhostingguy
 
PPTX
Bootstrap PPT by Mukesh
Mukesh Kumar
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PPT
Php Presentation
Manish Bothra
 
PPT
Visual basic 6.0
Aarti P
 
PPTX
Sessions in php
Mudasir Syed
 
PPTX
Server Scripting Language -PHP
Deo Shao
 
PPTX
Javascript 101
Shlomi Komemi
 
PPTX
PHP Cookies and Sessions
Nisa Soomro
 
PPT
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Phpmyadmin administer mysql
Mohd yasin Karim
 
PPTX
PHP COOKIES AND SESSIONS
Degu8
 
PDF
Bootstrap
Jadson Santos
 
3. Java Script
Jalpesh Vasa
 
Cookies and sessions
Sukrit Gupta
 
Javascript
mussawir20
 
Java Script ppt
Priya Goyal
 
Cookies and sessions
Lena Petsenchuk
 
jQuery for beginners
Arulmurugan Rajaraman
 
MySql slides (ppt)
webhostingguy
 
Bootstrap PPT by Mukesh
Mukesh Kumar
 
PHP FUNCTIONS
Zeeshan Ahmed
 
Php Presentation
Manish Bothra
 
Visual basic 6.0
Aarti P
 
Sessions in php
Mudasir Syed
 
Server Scripting Language -PHP
Deo Shao
 
Javascript 101
Shlomi Komemi
 
PHP Cookies and Sessions
Nisa Soomro
 
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Phpmyadmin administer mysql
Mohd yasin Karim
 
PHP COOKIES AND SESSIONS
Degu8
 
Bootstrap
Jadson Santos
 

Viewers also liked (20)

PDF
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Introduction to PHP
Bradley Holt
 
PPT
Class 6 - PHP Web Programming
Ahmed Swilam
 
PPSX
Sessions and cookies
www.netgains.org
 
ODP
PHP Web Programming
Muthuselvam RS
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
ODP
Coding In Php
Harit Kothari
 
PPT
Php Coding Convention
Phuong Vy
 
PDF
開始網站設計
James Shieh
 
PDF
MySQL資料庫設計規劃與phpMyAdmin
James Shieh
 
PDF
PHP an intro -1
Kanchilug
 
PPT
Php ssession - cookies -introduction
Programmer Blog
 
PPSX
PHP Comprehensive Overview
Mohamed Loey
 
PDF
Php course-syllabus
Himanshu Himanshu
 
PDF
AI Chatbot Service Framework based on Backpropagation Network for Predicting ...
James Shieh
 
PPT
Pakistan's mountain ranges
tehseen bukhari
 
PPT
PHP Cookies, Sessions and Authentication
Gerard Sychay
 
DOCX
Mountains In Pakistan
Ayesha Shoukat
 
PDF
Php & mysql course syllabus
Papitha Velumani
 
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to PHP
Bradley Holt
 
Class 6 - PHP Web Programming
Ahmed Swilam
 
Sessions and cookies
www.netgains.org
 
PHP Web Programming
Muthuselvam RS
 
Coding In Php
Harit Kothari
 
Php Coding Convention
Phuong Vy
 
開始網站設計
James Shieh
 
MySQL資料庫設計規劃與phpMyAdmin
James Shieh
 
PHP an intro -1
Kanchilug
 
Php ssession - cookies -introduction
Programmer Blog
 
PHP Comprehensive Overview
Mohamed Loey
 
Php course-syllabus
Himanshu Himanshu
 
AI Chatbot Service Framework based on Backpropagation Network for Predicting ...
James Shieh
 
Pakistan's mountain ranges
tehseen bukhari
 
PHP Cookies, Sessions and Authentication
Gerard Sychay
 
Mountains In Pakistan
Ayesha Shoukat
 
Php & mysql course syllabus
Papitha Velumani
 
Ad

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

PPT
Lecture8 php page control by okello erick
okelloerick
 
PDF
ASP.NET-Web Programming - Sessions and Cookies
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Module-5_WTA_Managing State & jQuery
SIVAKUMAR V
 
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
SreejithVP7
 
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
pondypaiyan
 
PDF
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
PDF
Web app development_cookies_sessions_14
Hassen Poreya
 
PPTX
Session tracking in servlets
vishal choudhary
 
PPTX
19_JavaScript - Storage_Cookies-tutorial .pptx
ssuser4a97d3
 
PPTX
Php file upload, cookies & session
Jamshid Hashimi
 
PPTX
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
PPTX
Authentication in Svelte using cookies.pptx
Knoldus Inc.
 
PDF
4.4 PHP Session
Jalpesh Vasa
 
PPTX
lecture 12.pptx
ITNet
 
ODP
Session Management & Cookies In Php
Harit Kothari
 
PPT
16 cookies
Abhijit Gaikwad
 
PPTX
javaScriptCookies.pptx
MattMarino13
 
PPT
season management in php (WT)
kunjan shah
 
PPTX
Ch4(saving state with cookies and query strings)
Chhom Karath
 
PPT
Php 07-cookies-sessions
YUSRA FERNANDO
 
Lecture8 php page control by okello erick
okelloerick
 
ASP.NET-Web Programming - Sessions and Cookies
baabtra.com - No. 1 supplier of quality freshers
 
Module-5_WTA_Managing State & jQuery
SIVAKUMAR V
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
SreejithVP7
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
pondypaiyan
 
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
Web app development_cookies_sessions_14
Hassen Poreya
 
Session tracking in servlets
vishal choudhary
 
19_JavaScript - Storage_Cookies-tutorial .pptx
ssuser4a97d3
 
Php file upload, cookies & session
Jamshid Hashimi
 
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
Authentication in Svelte using cookies.pptx
Knoldus Inc.
 
4.4 PHP Session
Jalpesh Vasa
 
lecture 12.pptx
ITNet
 
Session Management & Cookies In Php
Harit Kothari
 
16 cookies
Abhijit Gaikwad
 
javaScriptCookies.pptx
MattMarino13
 
season management in php (WT)
kunjan shah
 
Ch4(saving state with cookies and query strings)
Chhom Karath
 
Php 07-cookies-sessions
YUSRA FERNANDO
 
Ad

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

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

Recently uploaded (20)

PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“ONNX and Python to C++: State-of-the-art Graph Compilation,” a Presentation ...
Edge AI and Vision Alliance
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
PPTX
Essential Content-centric Plugins for your Website
Laura Byrne
 
PDF
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“ONNX and Python to C++: State-of-the-art Graph Compilation,” a Presentation ...
Edge AI and Vision Alliance
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
Essential Content-centric Plugins for your Website
Laura Byrne
 
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 

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: [email protected]