SlideShare a Scribd company logo
1 of 26
Cookies

      Set a cookie – setcookie()
Extract data from a cookie - $_COOKIE
 Augment user authentication script
             with a cookie
What are cookies?
• Web transactions are “memory-less”
• A cookie is a text file that a website stores on a
  client’s computer to maintain information about
  the client during and between browsing sessions.
• Useful for:
   – Shopping carts
   – User communities
   – Personalized sites
• Not recommended for storing sensitive data
• Store a unique identification string that will
  match a user held securely in a database
Shopping example
• Assign an identification variable to a user to track
   what he does when he visits your site
1. User logs in
2. Send a cookie with variables to say “This is Joe, and Joe
   is authenticated”
3. While Joe is surfing your site, you can respond “Hello,
   Joe!” on every page
4. If Joe clicks through your catalog and chooses 3 items
   to buy, you can keep track of these items
5. Display the items together when Joe goes to the
   checkout area
Setting Cookies
•    A server can access only the cookies that it has placed on the client.
•    setcookie() function with parameters:
1.   Name – cookie name accessible in subsequent scripts
2.   Value – cookie value passed to name
3.   Expiration– (optional) sets a specific time in seconds when the cookie
     values is no longer accessible e.g. time() + 24*60*60*3 to expire in 3 days
      –    A cookie without expiration is known as a session cookie,
      –    A cookie with an expiration time is a persistent cookie.
1. Path – Directories the cookie is valid
      –    "/“ valid for all files and directories in the website
      –    Specific directory: cookie valid for pages within that directory
1. Domain- only valid for the host and domain that set them
      –    If no domain, host name of the server that generated the cookie
6. Security –
      –    1 or TRUE: cookie will only be transmitted via HTTPS i.e. secure web site
      –    0 or FALSE: non-secure
Example
• setcookie(   “id”,
               “55adb984523afer”,
               time() + 14400,
               “/”,
               “yourdomain.com”,
               0);
  // 4 hours
Bad cookie setting
• Cookies defined in function setcookie are
  sent to the client at the same time as the
  information in the HTTP header; therefore, it
  needs to be called before any XHTML is
  printed.
• Hence you absolutely must set a cookie
  before sending any other content to the
  browser
• See m16/bad_cookie.php
<html>
 <head>
   <title>Bad Cookie</title>
 </head>

 <body>
  <?php
  setcookie("test", "ok", "", "/", "127.0.0.1", 0);
  ?>

   <h1>Bad Cookie</h1>
 </body>
</html>

This is an error in setting cookies.
Setcookie() function should be placed before <html> tag
M16/bad_cookie.php
M16/16-1setcookie.php
<?php
$cookie_name = "test_cookie";
$cookie_value = "test string!";
$cookie_expire = time()+86400;
$cookie_domain = "127.0.0.1";

setcookie($cookie_name, $cookie_value, $cookie_expire, "/" , $cookie_domain, 0);

?>
<html>
<head>
<title>Set Test Cookie</title>
</head>
<body>

<h1>Mmmmmmmm...cookie!</h1>

</body>
</html>
M16/16-1setcookie.php
Permanent cookie
• See fig23_16_20 from text
  – Cookies.html
  – Cookies.php
  – Readcookies.php
Input for cookies – cookies.html
Acknowledgment – cookies.php
Read cookies – readcookies.php
Augmenting auth_user with cookie
• 16-2show_login.php
  – Gets login username and password
  – Calls 16-2do_authuser.php to authenticate the login
• 16-2do_authuser.php
  – Checks DB to authenticate the login
  – If authenticated:
     • Set cookie for the user
     • displays links to
         – secretA.php
         – secretB.php
16-2show_login.html
Authenticated!
Clicking on secretA or secretB link . . .
• We would expect to get into the links
Wait! We got redirected back to the login page

• Why?
• Debug . . .
In 16-2do_authuser.php:

if ($num !=0){
         $cookie_name ="auth";
         $cookie_value ="ok";
         $cookie_expire ="0";
         $cookie_domain ="127.0.0.1";

        setcookie($cookie_name,$cookie_value,$cookie_expire,"/",
$cookie_domain,0);


The domain was 127.0.0.1

When we accessed it with http://localhost/m16/16-2secretB.php
Repeat the script in 127.0.0.1
Authenticated!
Clicking on the secretA link
Clicking on the secretB link
Check if cookie really works

• Exit the session
  – Exit completely out of the web browser
     • The cookie was a session cookie
     • Auth cookie should now have expired
  – Reopen the web browser
  – Attempt to access 16-2secretB.php
  – Since the user is not authenticated anymore, the
    user will be redirected to the login page
http://127.0.0.1/m16/16-2secretB.php leads to

More Related Content

What's hot

Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NET
ShingalaKrupa
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
UdaAs PaNchi
 

What's hot (20)

PHP - Getting good with cookies
PHP - Getting good with cookiesPHP - Getting good with cookies
PHP - Getting good with cookies
 
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
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NET
 
Cookies and Session
Cookies and SessionCookies and Session
Cookies and Session
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
java Cookies
java Cookiesjava Cookies
java Cookies
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Internet Cookies
Internet CookiesInternet Cookies
Internet Cookies
 
Presentation on Internet Cookies
Presentation on Internet CookiesPresentation on Internet Cookies
Presentation on Internet Cookies
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Cookie
CookieCookie
Cookie
 
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
 
Internet cookies
Internet cookiesInternet cookies
Internet cookies
 
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
 
Internet cookies
Internet cookiesInternet cookies
Internet cookies
 

Viewers also liked

Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community
StarBuzz Weekly
 

Viewers also liked (18)

12 cache questions
12 cache questions12 cache questions
12 cache questions
 
Sinh vienit.net --bao-cao-design_patterns
Sinh vienit.net --bao-cao-design_patternsSinh vienit.net --bao-cao-design_patterns
Sinh vienit.net --bao-cao-design_patterns
 
17 sessions
17 sessions17 sessions
17 sessions
 
Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community
 
8 polymorphism
8 polymorphism8 polymorphism
8 polymorphism
 
Decorator
DecoratorDecorator
Decorator
 
Design Patterns Course
Design Patterns CourseDesign Patterns Course
Design Patterns Course
 
Phani Kumar - Decorator Pattern
Phani Kumar - Decorator PatternPhani Kumar - Decorator Pattern
Phani Kumar - Decorator Pattern
 
Presenter and Decorator in Rails
Presenter and Decorator in RailsPresenter and Decorator in Rails
Presenter and Decorator in Rails
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)
 
15 decorator pattern
15 decorator pattern15 decorator pattern
15 decorator pattern
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 
12 memory hierarchy
12 memory hierarchy12 memory hierarchy
12 memory hierarchy
 
Design Pattern lecture 3
Design Pattern lecture 3Design Pattern lecture 3
Design Pattern lecture 3
 
Decorator Design Pattern
Decorator Design PatternDecorator Design Pattern
Decorator Design Pattern
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
 
Observer and Decorator Pattern
Observer and Decorator PatternObserver and Decorator Pattern
Observer and Decorator Pattern
 

Similar to 16 cookies

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
 
19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptx19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptx
VatsalJain39
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14
Hassen Poreya
 
Cookie replay attack unit wise presentation
Cookie replay attack  unit wise presentationCookie replay attack  unit wise presentation
Cookie replay attack unit wise presentation
Nilu Desai
 
07 cookies
07 cookies07 cookies
07 cookies
snopteck
 

Similar to 16 cookies (20)

PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONS
 
Php cookies
Php cookiesPhp cookies
Php cookies
 
Advance java session 7
Advance java session 7Advance java session 7
Advance java session 7
 
Cookies authentication
Cookies authenticationCookies authentication
Cookies authentication
 
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
 
APEX & Cookie Monster
APEX & Cookie MonsterAPEX & Cookie Monster
APEX & Cookie Monster
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptx
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
 
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)
 
Cookies
CookiesCookies
Cookies
 
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
 
19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptx19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptx
 
Overview of Cookies in HTTP - Miran al Mehrab
Overview of Cookies in HTTP - Miran al MehrabOverview of Cookies in HTTP - Miran al Mehrab
Overview of Cookies in HTTP - Miran al Mehrab
 
Cookies in php lecture 2
Cookies in php  lecture  2Cookies in php  lecture  2
Cookies in php lecture 2
 
javaScriptCookies.pptx
javaScriptCookies.pptxjavaScriptCookies.pptx
javaScriptCookies.pptx
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14
 
Cookie replay attack unit wise presentation
Cookie replay attack  unit wise presentationCookie replay attack  unit wise presentation
Cookie replay attack unit wise presentation
 
07 cookies
07 cookies07 cookies
07 cookies
 
15-auth-session-mgmt.ppt
15-auth-session-mgmt.ppt15-auth-session-mgmt.ppt
15-auth-session-mgmt.ppt
 
SSL and Wordpress
SSL and WordpressSSL and Wordpress
SSL and Wordpress
 

16 cookies

  • 1. Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie
  • 2. What are cookies? • Web transactions are “memory-less” • A cookie is a text file that a website stores on a client’s computer to maintain information about the client during and between browsing sessions. • Useful for: – Shopping carts – User communities – Personalized sites • Not recommended for storing sensitive data • Store a unique identification string that will match a user held securely in a database
  • 3. Shopping example • Assign an identification variable to a user to track what he does when he visits your site 1. User logs in 2. Send a cookie with variables to say “This is Joe, and Joe is authenticated” 3. While Joe is surfing your site, you can respond “Hello, Joe!” on every page 4. If Joe clicks through your catalog and chooses 3 items to buy, you can keep track of these items 5. Display the items together when Joe goes to the checkout area
  • 4. Setting Cookies • A server can access only the cookies that it has placed on the client. • setcookie() function with parameters: 1. Name – cookie name accessible in subsequent scripts 2. Value – cookie value passed to name 3. Expiration– (optional) sets a specific time in seconds when the cookie values is no longer accessible e.g. time() + 24*60*60*3 to expire in 3 days – A cookie without expiration is known as a session cookie, – A cookie with an expiration time is a persistent cookie. 1. Path – Directories the cookie is valid – "/“ valid for all files and directories in the website – Specific directory: cookie valid for pages within that directory 1. Domain- only valid for the host and domain that set them – If no domain, host name of the server that generated the cookie 6. Security – – 1 or TRUE: cookie will only be transmitted via HTTPS i.e. secure web site – 0 or FALSE: non-secure
  • 5. Example • setcookie( “id”, “55adb984523afer”, time() + 14400, “/”, “yourdomain.com”, 0); // 4 hours
  • 6. Bad cookie setting • Cookies defined in function setcookie are sent to the client at the same time as the information in the HTTP header; therefore, it needs to be called before any XHTML is printed. • Hence you absolutely must set a cookie before sending any other content to the browser • See m16/bad_cookie.php
  • 7. <html> <head> <title>Bad Cookie</title> </head> <body> <?php setcookie("test", "ok", "", "/", "127.0.0.1", 0); ?> <h1>Bad Cookie</h1> </body> </html> This is an error in setting cookies. Setcookie() function should be placed before <html> tag
  • 9. M16/16-1setcookie.php <?php $cookie_name = "test_cookie"; $cookie_value = "test string!"; $cookie_expire = time()+86400; $cookie_domain = "127.0.0.1"; setcookie($cookie_name, $cookie_value, $cookie_expire, "/" , $cookie_domain, 0); ?> <html> <head> <title>Set Test Cookie</title> </head> <body> <h1>Mmmmmmmm...cookie!</h1> </body> </html>
  • 11. Permanent cookie • See fig23_16_20 from text – Cookies.html – Cookies.php – Readcookies.php
  • 12. Input for cookies – cookies.html
  • 14. Read cookies – readcookies.php
  • 15. Augmenting auth_user with cookie • 16-2show_login.php – Gets login username and password – Calls 16-2do_authuser.php to authenticate the login • 16-2do_authuser.php – Checks DB to authenticate the login – If authenticated: • Set cookie for the user • displays links to – secretA.php – secretB.php
  • 18. Clicking on secretA or secretB link . . . • We would expect to get into the links
  • 19. Wait! We got redirected back to the login page • Why? • Debug . . .
  • 20. In 16-2do_authuser.php: if ($num !=0){ $cookie_name ="auth"; $cookie_value ="ok"; $cookie_expire ="0"; $cookie_domain ="127.0.0.1"; setcookie($cookie_name,$cookie_value,$cookie_expire,"/", $cookie_domain,0); The domain was 127.0.0.1 When we accessed it with http://localhost/m16/16-2secretB.php
  • 21. Repeat the script in 127.0.0.1
  • 23. Clicking on the secretA link
  • 24. Clicking on the secretB link
  • 25. Check if cookie really works • Exit the session – Exit completely out of the web browser • The cookie was a session cookie • Auth cookie should now have expired – Reopen the web browser – Attempt to access 16-2secretB.php – Since the user is not authenticated anymore, the user will be redirected to the login page