Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 84 lines (64 sloc) 4.011 kB
4774989 @irrelative edit
irrelative authored
1 ---
2 layout: default
3 title: cookies
4 ---
5
6 # cookies
7
8ed7ace Add link to french translation
nux authored
8 Other languages: [français](/../cookbook/cookies/fr) | ...
9
4774989 @irrelative edit
irrelative authored
10 ##Problem
11 You want to set and retrieve cookies for a user browsing the site.
12
13 ##Solution
c9caba4 @adamcrosby Added more setcookie() fields to reflect actual function definition.
adamcrosby authored
14 Web.py *comes* with an easy to use method for setting/getting cookies.
4774989 @irrelative edit
irrelative authored
15
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
16 ###Setting Cookies
17 ####Overview
c9caba4 @adamcrosby Added more setcookie() fields to reflect actual function definition.
adamcrosby authored
18 setcookie(name, value, expires="", domain=None, secure=False):
19
94d44ea @adamcrosby edit
adamcrosby authored
20 * *name* `(string)` - The actual name of the cookie, as stored by the browser, and returned to the server.
21 * *value* `(string)` - The value you want stored under that name.
22 * *expires* `(int)` - Optionally, is the time in seconds until the browser should expire the cookie. Note: this must be an integer, not a string.
23 * *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`).
24 * *secure* `(bool)`- If True, require that the cookie only be sent over HTTP/S.
4774989 @irrelative edit
irrelative authored
25
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
26 ####Example
27 `web.setcookie()` can be used to set the cookie for a user, like this:
4774989 @irrelative edit
irrelative authored
28
29 class CookieSet:
30 def GET(self):
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
31 i = web.input(age='25')
32 web.setcookie('age', i.age, 3600)
33 return "Age set in your cookie"
34
4774989 @irrelative edit
irrelative authored
35
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
36 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
37
1c9bebf @xrfang small typo
xrfang authored
38 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
39
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
40 ###Retrieving Cookies
41 ####Overview
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
42 There are many methods to retrieve cookies, depending on the desired reaction to a missing cookie.
43 #####Way 1 (return None if cookie is not found):
558edc7 @adamcrosby edit
adamcrosby authored
44 web.cookies().get(cookieName)
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
45 #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
46 #####Way 2 (raises exception AttributeError if cookie is not found):
47 foo = web.cookies()
48 foo.cookieName
49 #####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 s…
adamcrosby authored
50 foo = web.cookies(cookieName=defaultValue)
51 foo.cookieName # return the value (which could be default)
52 #cookieName is the name of the cookie submitted by the browser
53
54 ####Example
55 `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
56
57 class CookieGet:
58 def GET(self):
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
59 c = web.cookies(age="25")
60 return "Your age is: " + c.age
61
62 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
63
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
64 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
65
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
66 class CookieGet:
67 def GET(self):
68 try:
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
69 return "Your age is: " + web.cookies().age
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
70 except:
71 # Do whatever handling you need to, etc. here.
72 return "Cookie does not exist."
4074d92 @adamcrosby fixed text to match example, provided summary/prototype
adamcrosby authored
73
352b823 @scq edit
scq authored
74 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.
75
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
76 or
77
78 class CookieGet:
79 def GET(self):
5e5b1cf @EcmaXp web.cookies().get(some) change
EcmaXp authored
80 age=web.cookies().get('age')
42c3d40 @marczz Fixed an error in use of get on a storage object
marczz authored
81 if age:
82 return "Your age is: %s" % age
83 else:
352b823 @scq edit
scq authored
84 return "Cookie does not exist."
Something went wrong with that request. Please try again.