Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 82 lines (63 sloc) 3.948 kb
4774989 @irrelative edit
irrelative authored
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
c9caba4 @adamcrosby Added more setcookie() fields to reflect actual function definition.
adamcrosby authored
12 Web.py *comes* with an easy to use method for setting/getting cookies.
4774989 @irrelative edit
irrelative authored
13
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
14 ###Setting Cookies
15 ####Overview
c9caba4 @adamcrosby Added more setcookie() fields to reflect actual function definition.
adamcrosby authored
16 setcookie(name, value, expires="", domain=None, secure=False):
17
94d44ea @adamcrosby edit
adamcrosby authored
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.
4774989 @irrelative edit
irrelative authored
23
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
24 ####Example
25 `web.setcookie()` can be used to set the cookie for a user, like this:
4774989 @irrelative edit
irrelative authored
26
27 class CookieSet:
28 def GET(self):
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
29 i = web.input(age='25')
30 web.setcookie('age', i.age, 3600)
31 return "Age set in your cookie"
32
4774989 @irrelative edit
irrelative authored
33
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
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).
4774989 @irrelative edit
irrelative authored
35
1c9bebf @xrfang small typo
xrfang authored
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).
4774989 @irrelative edit
irrelative authored
37
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
38 ###Retrieving Cookies
39 ####Overview
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
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):
558edc7 @adamcrosby edit
adamcrosby authored
42 web.cookies().get(cookieName)
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
43 #cookieName is the name of the cookie submitted by the browser
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
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):
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
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:
4774989 @irrelative edit
irrelative authored
54
55 class CookieGet:
56 def GET(self):
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
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.
4774989 @irrelative edit
irrelative authored
61
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
62 Sometimes, you want to know specifically if something doesn't exist, in which case you can use something like the following:
4074d92 @adamcrosby fixed text to match example, provided summary/prototype
adamcrosby authored
63
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
64 class CookieGet:
65 def GET(self):
66 try:
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
67 return "Your age is: " + web.cookies().age
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding some...
adamcrosby authored
68 except:
69 # Do whatever handling you need to, etc. here.
70 return "Cookie does not exist."
4074d92 @adamcrosby fixed text to match example, provided summary/prototype
adamcrosby authored
71
352b823 @scq edit
scq authored
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
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
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:
352b823 @scq edit
scq authored
82 return "Cookie does not exist."
Something went wrong with that request. Please try again.