Copyright ©2005 Department of Computer & Information Science Working with Cookies

Preview:

Citation preview

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Working with CookiesWorking with Cookies

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

GoalsGoals

By the end of this lecture you By the end of this lecture you should …should …

• Understand the different types of Understand the different types of cookies.cookies.

• Understand how JavaScript keeps Understand how JavaScript keeps track of cookies using the track of cookies using the document.cookie object.document.cookie object.

continued …continued …

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

GoalsGoals

By the end of this lecture you should …By the end of this lecture you should …• Understand how to create new Understand how to create new

cookies.cookies.• Understand how to set an expiration Understand how to set an expiration

date for a cookie.date for a cookie.• Understand how to delete a cookie.Understand how to delete a cookie.• Understand how to update a cookie’s Understand how to update a cookie’s

value.value.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

What is a Cookie?What is a Cookie?

• A cookie is a small text file that A cookie is a small text file that JavaScript can use to store JavaScript can use to store customized information about a customized information about a user.user.

• There are two types of cookies:There are two types of cookies:– Session Cookies (aka Transient Cookies)Session Cookies (aka Transient Cookies)– Persistent CookiesPersistent Cookies

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Session CookiesSession Cookies

• A browser stores session cookies A browser stores session cookies in memory. in memory.

• Once a browser session ends, Once a browser session ends, browser loses the contents of a browser loses the contents of a session cookie.session cookie.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Persistent CookiesPersistent Cookies

• Browsers store persistent Browsers store persistent cookies to a user’s hard drive.cookies to a user’s hard drive.

• We can use persistent cookies to We can use persistent cookies to customize information about a customize information about a user that we can use when the user that we can use when the user returns to a website at a user returns to a website at a later date.later date.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Cookies as ObjectsCookies as Objects

• JavaScript deals with cookies as JavaScript deals with cookies as objects. objects.

• Specifically, JavaScript works with Specifically, JavaScript works with cookies using the cookies using the document.cookiedocument.cookie attribute.attribute.

• We can read information from We can read information from cookies by examining the cookies by examining the document.cookiedocument.cookie object. object.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Examples of Cookie UsesExamples of Cookie Uses

• User preferencesUser preferences• Saving form dataSaving form data• Assisting with conveying Assisting with conveying

information to back-end information to back-end programsprograms

• Tracking online shopping habitsTracking online shopping habits

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Parts of a Cookie ObjectParts of a Cookie Object

• namename – An identifier by which we – An identifier by which we reference a particular cookie.reference a particular cookie.

• valuevalue – The information we wish to – The information we wish to save, in reference to a particular save, in reference to a particular cookie.cookie.

• expiresexpires – A GMT-formatted date – A GMT-formatted date specifying the date (in milliseconds) specifying the date (in milliseconds) when a cookie will expire.when a cookie will expire.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Parts of a Cookie ObjectParts of a Cookie Object

• pathpath – Specifies the path of the web server in – Specifies the path of the web server in which the cookie is valid. By default, set to which the cookie is valid. By default, set to the path of the page that set the cookie. the path of the page that set the cookie. However, commonly specified to However, commonly specified to //, the root , the root directory.directory.

• domaindomain – Specifies the domain for which the – Specifies the domain for which the cookie is valid. Set, by default, only to the full cookie is valid. Set, by default, only to the full domain of a page. You may wish to extend domain of a page. You may wish to extend the domain to include other computers in the domain to include other computers in your domain.your domain.

• securesecure – Specifies that a web browser needs – Specifies that a web browser needs a secure HTTP connection to access a cookie.a secure HTTP connection to access a cookie.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Cookie LimitationsCookie Limitations

• A given domain may only set 20 A given domain may only set 20 cookies per machine.cookies per machine.

• A single browser may only store A single browser may only store 300 cookies.300 cookies.

• Browsers limit a single cookie to Browsers limit a single cookie to 4KB.4KB.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Setting a Cookie – General Setting a Cookie – General FormForm

window.document.cookie = window.document.cookie =

““cookieName = cookieValuecookieName = cookieValue[; expires = expireDate][; expires = expireDate][; path = pathName][; path = pathName][; domain = domainName][; domain = domainName][; secure]”;[; secure]”;

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Escape SequencesEscape Sequences

• When we set cookie values, we must first When we set cookie values, we must first convert the string values that set a cookie convert the string values that set a cookie so that the string doesn’t contain white so that the string doesn’t contain white space, commas or semi-colons. space, commas or semi-colons.

• We can use JavaScript’s intrinsic We can use JavaScript’s intrinsic escape() escape() function to convert white space and function to convert white space and punctuation with escape sequences. punctuation with escape sequences.

• Conversely, we can use Conversely, we can use unescape() unescape() to to view text encoded with view text encoded with escape()escape()..

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Using the Cookie LibraryUsing the Cookie Library

• A great, open-source, tool is Bill A great, open-source, tool is Bill Dortch’s Cookie Library, which Dortch’s Cookie Library, which contains a plethora of useful contains a plethora of useful functions for dealing with cookies. functions for dealing with cookies.

• To use the functions, include the To use the functions, include the syntax on the next slide in your syntax on the next slide in your scripts …scripts …

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Including the Cookie LibraryIncluding the Cookie Library

<script language=“JavaScript 2.1”<script language=“JavaScript 2.1”type = “text/javascript”type = “text/javascript”src = src =

“cookieLibrary.js”>“cookieLibrary.js”>

</script></script>

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Calculation Expiration DatesCalculation Expiration Dates

• JavaScript stores dates as the number JavaScript stores dates as the number of milliseconds since 01/01/1970. of milliseconds since 01/01/1970.

• To calculate a new date, we would use To calculate a new date, we would use a formula derived from milliseconds.a formula derived from milliseconds.

• For example, 1 year in milliseconds:For example, 1 year in milliseconds:year = (365 * 24 * 60 * 60 * year = (365 * 24 * 60 * 60 *

1000) 1000)

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Calculating Expiration DatesCalculating Expiration Dates

var dNow = new Date();var dNow = new Date();var intTime = new Number();var intTime = new Number();intTime = dNow.getTime() + intTime = dNow.getTime() +

((365365 * 24 * 60 * * 24 * 60 *60 * 1000);60 * 1000);

var dExpire = var dExpire = new Date(intTime);new Date(intTime);

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Calling Calling SetCookie()SetCookie()

• We can call the We can call the SetCookie()SetCookie() function from the Cookie Library function from the Cookie Library using the following syntax:using the following syntax:SetCookie(name, value SetCookie(name, value

[, expires] [, expires] [, path] [, path] [, domain] [, domain] [, secure]); [, secure]);

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Open the file called usingCookies_01.html

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Calling Calling GetCookie()GetCookie()

• We can call the We can call the GetCookie()GetCookie() function from the Cookie Library function from the Cookie Library using the following syntax:using the following syntax:GetCookie(cookieName);GetCookie(cookieName);

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Open the file called usingCookies_02.html

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Calling Calling DeleteCookie()DeleteCookie()

• We can call the We can call the DeleteCookie()DeleteCookie() function from the Cookie Library function from the Cookie Library using the following syntax:using the following syntax:DeleteCookie(name, DeleteCookie(name,

[, path] [, path] [, domain]); [, domain]);

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Open the file called usingCookies_03.html

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Modifying a CookieModifying a Cookie

• We can call the We can call the SetCookie()SetCookie() function from the Cookie Library function from the Cookie Library to modify a cookie, essentially to modify a cookie, essentially overwriting the previous cookie.overwriting the previous cookie.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

Open the file called usingCookies_04.html

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

SummarySummary

• Session cookies are available to a Session cookies are available to a browser so long as a browser session browser so long as a browser session is active; once the browser closes, it is active; once the browser closes, it loses all session cookies.loses all session cookies.

• Browsers save persistent cookies to a Browsers save persistent cookies to a user’s hard drive; the values of user’s hard drive; the values of persistent cookies are available persistent cookies are available across multiple browser sessions.across multiple browser sessions.

continued …continued …

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

SummarySummary

• Cookies consist of a name, value, Cookies consist of a name, value, expiration date, path, domain expiration date, path, domain and secure flag.and secure flag.

• Browsers store cookie Browsers store cookie modification dates in modification dates in milliseconds, from 01/01/1970. milliseconds, from 01/01/1970.

continued …continued …

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

SummarySummary

• We can use the SetCookie() function We can use the SetCookie() function from the open-source Cookie Library from the open-source Cookie Library to set or modify a cookie.to set or modify a cookie.

• We can use the GetCookie() function We can use the GetCookie() function from the open-source Cookie Library from the open-source Cookie Library to retrieve an existing cookie’s value.to retrieve an existing cookie’s value.

• We can use the DeleteCookie() We can use the DeleteCookie() function from the open-source Cookie function from the open-source Cookie Library to delete a cookie.Library to delete a cookie.

Copyright Copyright ©2005 ©2005 Department of Computer & Information Department of Computer & Information ScienceScience

ResourcesResources

• Spain-McDuffie, Tina. Spain-McDuffie, Tina. JavaScript JavaScript Concepts & Techniques: Concepts & Techniques: Programming Interactive Web Programming Interactive Web SitesSites. Wilsonville, OR: Franklin, . Wilsonville, OR: Franklin, Beedle & Associates, 2003.Beedle & Associates, 2003.

Recommended