Skip to content
This repository
Newer
Older
100644 82 lines (63 sloc) 3.948 kb
47749896 » irrelative
2008-03-01 edit
1 ---
2 layout: default
3 title: cookies
4 ---
5
6 # cookies
7
8 ##Problem
9 You want to set and retrieve cookies for a user browsing the site.
10
11 ##Solution
c9caba42 » adamcrosby
2008-08-07 Added more setcookie() fields to reflect actual function definition.
12 Web.py *comes* with an easy to use method for setting/getting cookies.
47749896 » irrelative
2008-03-01 edit
13
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
14 ###Setting Cookies
15 ####Overview
c9caba42 » adamcrosby
2008-08-07 Added more setcookie() fields to reflect actual function definition.
16 setcookie(name, value, expires="", domain=None, secure=False):
17
94d44eaf » adamcrosby
2008-08-07 edit
18 * *name* `(string)` - The actual name of the cookie, as stored by the browser, and returned to the server.
19 * *value* `(string)` - The value you want stored under that name.
20 * *expires* `(int)` - Optionally, is the time in seconds until the browser should expire the cookie. Note: this must be an integer, not a string.
21 * *domain* `(string)` - The domain the cookie is valid for. By default, set to the host accessed, this allows you to set the domain, rather than just a host (such as `.webpy.org`).
22 * *secure* `(bool)`- If True, require that the cookie only be sent over HTTP/S.
47749896 » irrelative
2008-03-01 edit
23
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
24 ####Example
25 `web.setcookie()` can be used to set the cookie for a user, like this:
47749896 » irrelative
2008-03-01 edit
26
27 class CookieSet:
28 def GET(self):
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
29 i = web.input(age='25')
30 web.setcookie('age', i.age, 3600)
31 return "Age set in your cookie"
32
47749896 » irrelative
2008-03-01 edit
33
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
34 Calling the above class with GET will set a cookie named "age" with a default value equal to "25" (this default value actually comes from the web.input processing, NOT the setcookie function), which expires in 1 hour (3600 seconds).
47749896 » irrelative
2008-03-01 edit
35
1c9bebf8 » xrfang
2009-11-06 small typo
36 The third (and optional) argument to `web.setcookie()`, "expires", allows you to set when you want your cookie to expire. Any negative number will expire the cookie immediately. Any positive number is the number of seconds that the cookie will last (3600 would result in an hour long cookie). Leaving this argument empty results in a session cookie, which expires when your browser shuts down. To make the cookie "permanent", simply update cookie expiration time at regular interval (e.g. when user logged in).
47749896 » irrelative
2008-03-01 edit
37
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
38 ###Retrieving Cookies
39 ####Overview
42c3d409 » marczz
2009-01-03 Fixed an error in use of get on a storage object
40 There are many methods to retrieve cookies, depending on the desired reaction to a missing cookie.
41 #####Way 1 (return None if cookie is not found):
558edc7d » adamcrosby
2008-08-07 edit
42 web.cookies().get(cookieName)
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
43 #cookieName is the name of the cookie submitted by the browser
42c3d409 » marczz
2009-01-03 Fixed an error in use of get on a storage object
44 #####Way 2 (raises exception AttributeError if cookie is not found):
45 foo = web.cookies()
46 foo.cookieName
47 #####Way 3 (avoids exception by setting default value for cookie if not found):
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
48 foo = web.cookies(cookieName=defaultValue)
49 foo.cookieName # return the value (which could be default)
50 #cookieName is the name of the cookie submitted by the browser
51
52 ####Example
53 `web.cookies()` can be used to access an already set cookie. If a cookie is set using the `web.setcookie()` code from above, it can be retrieved like this:
47749896 » irrelative
2008-03-01 edit
54
55 class CookieGet:
56 def GET(self):
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
57 c = web.cookies(age="25")
58 return "Your age is: " + c.age
59
60 The example sets a default value for the cookie if it does not exist. The reason for setting a default value is that if the cookie is attempted to be accessed, but does not exist, `web.cookies()` raises an exception.
47749896 » irrelative
2008-03-01 edit
61
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
62 Sometimes, you want to know specifically if something doesn't exist, in which case you can use something like the following:
4074d92d » adamcrosby
2008-08-07 fixed text to match example, provided summary/prototype
63
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
64 class CookieGet:
65 def GET(self):
66 try:
42c3d409 » marczz
2009-01-03 Fixed an error in use of get on a storage object
67 return "Your age is: " + web.cookies().age
5c250348 » adamcrosby
2008-08-07 Hefty rewrite, clearing up some confusign variable names and adding s…
68 except:
69 # Do whatever handling you need to, etc. here.
70 return "Cookie does not exist."
4074d92d » adamcrosby
2008-08-07 fixed text to match example, provided summary/prototype
71
352b823a » scq
2009-12-09 edit
72 This code attempts to use the cookie submitted by the browser, but does not give it a default value. If the cookie doesn't exist, an exception is raised, and the `except` clause is executed, giving the server an opportunity to handle the lack of cookie.
73
42c3d409 » marczz
2009-01-03 Fixed an error in use of get on a storage object
74 or
75
76 class CookieGet:
77 def GET(self):
78 age=web.cookies().get(age)
79 if age:
80 return "Your age is: %s" % age
81 else:
352b823a » scq
2009-12-09 edit
82 return "Cookie does not exist."
Something went wrong with that request. Please try again.