Chapter 10 Maintaining State Information Using Cookies.

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

CookiesPHPMay-2007 : [‹#›] Maintaining State in PHP Part I - Cookies.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
6/10/2015Cookies1 What are Cookies? 6/10/2015Cookies2 How did they do that?
Chapter 10 Managing State Information Using Sessions.
©2009 Justin C. Klein Keane PHP Code Auditing Session 7 Sessions and Cookies Justin C. Klein Keane
Managing State Information. PHP State Information 2 Objectives Learn about state information Use hidden form fields to save state information Use query.
XP Tutorial 9 New Perspectives on JavaScript, Comprehensive1 Working with Cookies Managing Data in a Web Site Using JavaScript Cookies.
Chapter 10 Managing State Information PHP Programming with MySQL.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Objectives Learn about state information
 A cookie is a piece of text that a Web server can store on a user's hard disk.  Cookie data is simply name-value pairs stored on your hard disk by.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
IT533 Lectures Session Management in ASP.NET. Session Tracking 2 Personalization Personalization makes it possible for e-businesses to communicate effectively.
Cookies and Security Saving the “state”
JavaScript, Fourth Edition
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
Chapter 8 Cookies And Security JavaScript, Third Edition.
12/3/2012ISC329 Isabelle Bichindaritz1 PHP and MySQL Advanced Features.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Cookies & Session Web Technology
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Dynamic Programming with PHP (mktime), Cookies, SQL, Authentication.
PHP Programming with MySQL Slide 10-1 CHAPTER 10 Managing State Information.
Advance web Programming Managing State Information (Cookies-Session) Date: 22 April 2014 Advance web Programming Managing State Information (Cookies-Session)
COOKIES and SESSIONS. COOKIES 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.
Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.
Web Database Programming Week 7 Session Management & Authentication.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
PHP Cookies. Cookies are small files that are stored in the visitor's browser. Cookies can be used to identify return visitors, keep a user logged into.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
How to maintain state in a stateless web Shirley Cohen
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
 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.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Cookies and Sessions in PHP. Arguments for the setcookie() Function There are several arguments you can use i.e. setcookie(‘name’, ‘value’, expiration,
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Sessions and cookies (part 2) MIS 3501, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/19/2015.
1 Chapter 22 World Wide Web (HTTP) Chapter 22 World Wide Web (HTTP) Mi-Jung Choi Dept. of Computer Science and Engineering
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Programming for the Web Cookies & Sessions Dónal Mulligan BSc MA
CSE 154 Lecture 20: Cookies.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
ITM 352 Cookies.
Web Programming Language
Cookies and Sessions in PHP
Implementing Cookies in PHP
14-мавзу. Cookie, сеанс, FTP и технологиялари
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
<?php require("header.htm"); ?>
Cookies Cookie :- A cookie is often used to identify a user. A cookie is often used to identify a user. A cookie is a small file that the server embeds.
Web Programming Language
PHP State.
Web Programming Language
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

Chapter 10 Maintaining State Information Using Cookies

PHP Programming2 Understanding State Information State information State information –Information about individual visits to a Web site Stateless Stateless –Original design of HTTP –Every request for a Web page is a unique user session Maintaining state Maintaining state –Storing persistent information about Web site visits –May use hidden form fields, query strings, cookies or sessions

PHP Programming3 Maintaining State Information Customize individual Web pages based on user preferences Customize individual Web pages based on user preferences Temporarily store information for a user as a browser navigates within a multipart form Temporarily store information for a user as a browser navigates within a multipart form Keep track of how many times a user has visited a Web site Keep track of how many times a user has visited a Web site Provide shopping carts that store order information Provide shopping carts that store order information Store user IDs and passwords Store user IDs and passwords

PHP Programming4 Understanding State Information Figure 10-1 Skyward Aviation Frequent Flyer Web site page flow

Using Cookies to Save State Small pieces of information that can be exchanged between a client and a server Small pieces of information that can be exchanged between a client and a server Used to store state information beyond the current Web page session Used to store state information beyond the current Web page session Created by Netscape Created by Netscape

PHP Programming6 Using Cookies to Save State Temporary cookies Temporary cookies –Available only for the current browser session Persistent cookies Persistent cookies –Available beyond the current browser session –Stored in a text file on a client computer Limitations Limitations –A server or domain can not store more than 20 cookies on a user’s computer –Total cookies per browser cannot exceed 300 –The largest cookie size is 4 kilobytes

PHP Programming7 Creating Cookies setcookie() function setcookie() function setcookie(name [,value,expires, path, domain, secure]) setcookie(name [,value,expires, path, domain, secure]) Call setcookie() before sending the Web browser any output, including echo or print statements Call setcookie() before sending the Web browser any output, including echo or print statements Users may reject cookies Users may reject cookies –A value of true is returned even if a user rejects the cookie Only name is required Only name is required –use an empty string to omit value, path and domain –specify 0 to omit expires and secure arguments

PHP Programming8 name and value Cookies created with only the name and value arguments are temporary cookies Cookies created with only the name and value arguments are temporary cookies –Available for only the current browser session <?php setcookie(“name”, “Mickey Mouse”); ?> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “ “ <head> My Web Site My Web Site...

PHP Programming9 Creating Multiple Cookies The setcookie() function can be called multiple times to create additional cookies The setcookie() function can be called multiple times to create additional cookies setcookie("first", “Mickey"); setcookie("last", “Mouse"); setcookie("occupation", “actor");

PHP Programming10 The expires Argument Determines how long a cookie can remain on a client system before it is deleted Determines how long a cookie can remain on a client system before it is deleted If omitted, cookies are available for only the current browser session If omitted, cookies are available for only the current browser session To specify a cookie’s expiration time, use PHP’s time() function To specify a cookie’s expiration time, use PHP’s time() function setcookie(“name”, “Mickey”, time()+3600); setcookie(“name”, “Mickey”, time()+60*60*24*7); expires in one hour expires in one week

PHP Programming11 The path Argument The path argument determines the availability of a cookie to other Web pages on a server The path argument determines the availability of a cookie to other Web pages on a server –Allows cookies to be shared across a server A cookie is available to all Web pages in a specified path as well as all subdirectories in the specified path A cookie is available to all Web pages in a specified path as well as all subdirectories in the specified path setcookie(“name”, “Mickey”, time()+3600, “/marketing/”); setcookie(“name”, “Mickey”, time()+3600, “/”);

PHP Programming12 The domain Argument The domain argument is used for sharing cookies across multiple servers in the same domain The domain argument is used for sharing cookies across multiple servers in the same domain Cookies cannot be shared outside of a domain Cookies cannot be shared outside of a domain setcookie(“name”, “Mickey”, time()+3600, “/”, “.disney.com”);

PHP Programming13 The secure Argument Allows a cookie only to be transmitted across a secure Internet connection Allows a cookie only to be transmitted across a secure Internet connection –May use HTTPS or another security protocol Assign a value of 1 (for true) or 0 (for false) Assign a value of 1 (for true) or 0 (for false) setcookie(“name”, “Mickey”, time()+3600, “”, “”, 1);

PHP Programming14 Reading Cookies Cookies that are available to the current Web page are automatically assigned to the $_COOKIE autoglobal Cookies that are available to the current Web page are automatically assigned to the $_COOKIE autoglobal Use the cookie name as a key in the associative $_COOKIE[] array Use the cookie name as a key in the associative $_COOKIE[] array echo $_COOKIE['firstName']; Newly created cookies are not available until after the current Web page is reloaded Newly created cookies are not available until after the current Web page is reloaded –Part of HTTP header information –Sent from client to server when request is made

PHP Programming15 Reading Cookies To ensure that a cookie is set before you attempt to use it, use the isset() function To ensure that a cookie is set before you attempt to use it, use the isset() function setcookie("first", “Mickey"); setcookie("last", “Mouse"); setcookie("occupation", “actor"); if (isset($_COOKIE['first']) && isset($_COOKIE['last']) && isset($_COOKIE['last']) && isset($_COOKIE['occupation'])) && isset($_COOKIE['occupation'])) echo "{$_COOKIE['firstName']} {$_COOKIE['lastName']} echo "{$_COOKIE['firstName']} {$_COOKIE['lastName']} is a {$_COOKIE['occupation']}."; is a {$_COOKIE['occupation']}.";

PHP Programming16 Deleting Cookies To delete a cookie, call setcookie() with no value argument: To delete a cookie, call setcookie() with no value argument: –setcookie(“name”);

PHP Programming17 Fruit Enter your favorite fruit:&nbsp <?php if (!empty($_POST["fruit"])) { //make persistent cookie for one day setcookie("fruit",$_POST["fruit"], mktime()+60*60*24); } //redirect header("Location: showFruit.php"); ?> <?php if (isset($_COOKIE["fruit"])) echo "Your favorite fruit is ". $_COOKIE["fruit"]; else echo "I dunno your favorite fruit."; ?> getFruit.htm storeFruit.php showFruit.php

PHP Programming18 Example You set up a site and include a login, but don’t want to force the customer to log in every time they visit. You set up a site and include a login, but don’t want to force the customer to log in every time they visit. A cookie can be used to store customer login information and automatically log them in when they revisit the site. A cookie can be used to store customer login information and automatically log them in when they revisit the site.