SlideShare a Scribd company logo
4
Most read
17
Most read
21
Most read
Chapter 5
Cookies and Session
Contents
• Cookies and Sessions
• Describe the stateless model
• Explain the concepts of maintaining state with
sessions
• Create and Read data from sessions
PHP COOKIES AND SESSIONS
What is a Cookie?
• A cookie is often used to identify a user.
• A cookie is a small file that the server embeds on
the user's computer.
• Each time the same computer requests a page
with a browser, it will send the cookie too.
• With PHP, you can both create and retrieve
cookie values.
• Cookies are primarily used to store the user’s
browsing history.
Create Cookies With PHP
• A cookie is created with the setcookie() function.
• Syntax:
setcookie(name, value, expire, path, domain, security);
• Parameters: The setcookie() function requires six
arguments in general which are:
Parameters:
• Name: It is used to set the name of the cookie.
• Value: It is used to set the value of the cookie.
• Expire: It is used to set the expiry timestamp of the cookie after which the
cookie can’t be accessed.
• Path: It is used to specify the path on the server for which the cookie will be
available.
• Domain: It is used to specify the domain for which the cookie is available.
• Security: It is used to indicate that the cookie should be sent only if a secure
HTTPS connection exists.
Cont.
• The following example creates a cookie named "user" with the
value "John Doe".
• The cookie will expire after 30 days (86400 * 30).
• The "/" means that the cookie is available in entire website
(otherwise, select the directory you prefer).
• We then retrieve the value of the cookie "user" (using the
global variable $_COOKIE).
• We also use the isset() function to find out if the cookie is set:
Operations that can be performed on
Cookies in PHP:
Creating Cookies:
• Creating a cookie named Auction_Item and
assigning the value Luxury Car to it.
• The cookie will expire after 2 days(2 days * 24
hours * 60 mins * 60 seconds).
Example
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 30 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Checking Whether a Cookie Is Set Or Not
• It is always advisable to check whether a cookie is
set or not before accessing its value.
• Therefore to check whether a cookie is set or not,
the PHP isset() function is used.
• To check whether a cookie “Auction_Item” is set
or not, the isset() function is executed as follows:
Example
<!DOCTYPE html>
<?php
setcookie("Auction_Item", "Luxury Car", time() + 2 * 24 * 60 * 60);
?>
<html>
<body>
<?php
if (isset($_COOKIE["Auction_Ite"]))
{
echo "Auction Item is a " . $_COOKIE["Auction_Item"];
}
else
{
echo "No items for auction.";
}
?>
</body>
</html>
Delete a Cookie
• To delete a cookie, use the setcookie() function with an
expiration date in the past:
• <?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
What is a PHP Session?
• When you work with an application, you open it, do
some changes, and then you close it.
• The computer knows who you are. It knows when you
start the application and when you end.
• But on the internet there is one problem: the web
server does not know who you are or what you do,
because the HTTP address doesn't maintain state.
Cont.
• Session variables solve this problem by storing
user information to be used across multiple
pages. By default, session variables last until the
user closes the browser.
• So; Session variables hold information about one
single user, and are available to all pages in one
application.
Start a PHP Session
• A session is started with
the session_start() function.
• Session variables are set with the PHP global
variable: $_SESSION.
• <?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
Get PHP Session Variable Values
• <?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>
Cont.
• Another way to show all the session variable values for a
user session is to run the following code:
• <?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>
Destroy a PHP Session
• To remove all global session variables and destroy the
session, use session_unset() and session_destroy():
• <?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
</body>
</html>
END

More Related Content

PDF
Introduction to php
PPT
Oops concepts in php
PPTX
Javascript alert and confrim box
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PDF
4.2 PHP Function
PPT
PHP - Introduction to PHP AJAX
PPT
Js ppt
Introduction to php
Oops concepts in php
Javascript alert and confrim box
PHP - DataType,Variable,Constant,Operators,Array,Include and require
4.2 PHP Function
PHP - Introduction to PHP AJAX
Js ppt

What's hot (20)

PDF
Bca sem 6 php practicals 1to12
PPTX
Exception Handling in C++
PPT
Php forms
PDF
Php array
PPTX
Javascript operators
PPTX
Client side scripting using Javascript
PPTX
Introduction to php
PDF
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
PPTX
PPTX
HTTP request and response
PPT
Servlet life cycle
PDF
jQuery for beginners
ODP
Multithreading In Java
PPTX
PPTX
PHP FUNCTIONS
PPTX
Flask – Python
PPT
Chapter 02 php basic syntax
PPTX
Operators php
PPTX
PHP Cookies and Sessions
PPSX
JDBC: java DataBase connectivity
Bca sem 6 php practicals 1to12
Exception Handling in C++
Php forms
Php array
Javascript operators
Client side scripting using Javascript
Introduction to php
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
HTTP request and response
Servlet life cycle
jQuery for beginners
Multithreading In Java
PHP FUNCTIONS
Flask – Python
Chapter 02 php basic syntax
Operators php
PHP Cookies and Sessions
JDBC: java DataBase connectivity
Ad

Similar to PHP COOKIES AND SESSIONS (20)

PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PDF
Web app development_cookies_sessions_14
PPT
Lecture8 php page control by okello erick
PDF
PHP-Cookies-Sessions.pdf
PPTX
FP512 Cookies sessions
PPSX
Sessions and cookies
PPTX
4 php-advanced
PPT
Session,cookies
PDF
4.4 PHP Session
PDF
Introduction to php web programming - sessions and cookies
PPTX
lecture 12.pptx
PPT
Php ssession - cookies -introduction
PPTX
PHP SESSIONS & COOKIE.pptx
ODP
Session Management & Cookies In Php
PPTX
Cookies and Session
PDF
PHP Making Web Forms
PPT
Manish
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Web app development_cookies_sessions_14
Lecture8 php page control by okello erick
PHP-Cookies-Sessions.pdf
FP512 Cookies sessions
Sessions and cookies
4 php-advanced
Session,cookies
4.4 PHP Session
Introduction to php web programming - sessions and cookies
lecture 12.pptx
Php ssession - cookies -introduction
PHP SESSIONS & COOKIE.pptx
Session Management & Cookies In Php
Cookies and Session
PHP Making Web Forms
Manish
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Institutional Correction lecture only . . .
PDF
RMMM.pdf make it easy to upload and study
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Computing-Curriculum for Schools in Ghana
Supply Chain Operations Speaking Notes -ICLT Program
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pre independence Education in Inndia.pdf
Complications of Minimal Access Surgery at WLH
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O7-L3 Supply Chain Operations - ICLT Program
Institutional Correction lecture only . . .
RMMM.pdf make it easy to upload and study
102 student loan defaulters named and shamed – Is someone you know on the list?
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

PHP COOKIES AND SESSIONS

  • 2. Contents • Cookies and Sessions • Describe the stateless model • Explain the concepts of maintaining state with sessions • Create and Read data from sessions
  • 4. What is a Cookie? • A cookie is often used to identify a user. • A cookie is a small file that the server embeds on the user's computer. • Each time the same computer requests a page with a browser, it will send the cookie too. • With PHP, you can both create and retrieve cookie values.
  • 5. • Cookies are primarily used to store the user’s browsing history.
  • 6. Create Cookies With PHP • A cookie is created with the setcookie() function. • Syntax: setcookie(name, value, expire, path, domain, security); • Parameters: The setcookie() function requires six arguments in general which are:
  • 7. Parameters: • Name: It is used to set the name of the cookie. • Value: It is used to set the value of the cookie. • Expire: It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed. • Path: It is used to specify the path on the server for which the cookie will be available. • Domain: It is used to specify the domain for which the cookie is available. • Security: It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.
  • 8. Cont. • The following example creates a cookie named "user" with the value "John Doe". • The cookie will expire after 30 days (86400 * 30). • The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer). • We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). • We also use the isset() function to find out if the cookie is set:
  • 9. Operations that can be performed on Cookies in PHP:
  • 10. Creating Cookies: • Creating a cookie named Auction_Item and assigning the value Luxury Car to it. • The cookie will expire after 2 days(2 days * 24 hours * 60 mins * 60 seconds).
  • 11. Example <?php $cookie_name = "user"; $cookie_value = "John Doe"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 30 day ?> <html> <body> <?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> </body> </html>
  • 12. Checking Whether a Cookie Is Set Or Not • It is always advisable to check whether a cookie is set or not before accessing its value. • Therefore to check whether a cookie is set or not, the PHP isset() function is used. • To check whether a cookie “Auction_Item” is set or not, the isset() function is executed as follows:
  • 13. Example <!DOCTYPE html> <?php setcookie("Auction_Item", "Luxury Car", time() + 2 * 24 * 60 * 60); ?> <html> <body> <?php if (isset($_COOKIE["Auction_Ite"])) { echo "Auction Item is a " . $_COOKIE["Auction_Item"]; } else { echo "No items for auction."; } ?> </body> </html>
  • 14. Delete a Cookie • To delete a cookie, use the setcookie() function with an expiration date in the past: • <?php // set the expiration date to one hour ago setcookie("user", "", time() - 3600); ?> <html> <body> <?php echo "Cookie 'user' is deleted."; ?> </body> </html>
  • 15. What is a PHP Session? • When you work with an application, you open it, do some changes, and then you close it. • The computer knows who you are. It knows when you start the application and when you end. • But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state.
  • 16. Cont. • Session variables solve this problem by storing user information to be used across multiple pages. By default, session variables last until the user closes the browser. • So; Session variables hold information about one single user, and are available to all pages in one application.
  • 17. Start a PHP Session • A session is started with the session_start() function. • Session variables are set with the PHP global variable: $_SESSION.
  • 18. • <?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variables are set."; ?> </body> </html>
  • 19. Get PHP Session Variable Values • <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Echo session variables that were set on previous page echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>"; echo "Favorite animal is " . $_SESSION["favanimal"] . "."; ?> </body> </html>
  • 20. Cont. • Another way to show all the session variable values for a user session is to run the following code: • <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php print_r($_SESSION); ?> </body> </html>
  • 21. Destroy a PHP Session • To remove all global session variables and destroy the session, use session_unset() and session_destroy(): • <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // remove all session variables session_unset(); // destroy the session session_destroy(); ?> </body> </html>
  • 22. END