Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 68 lines (50 sloc) 3.164 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
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
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 s…
adamcrosby authored
14 ###Setting Cookies
15 ####Overview
16
17 web.setcookie(cookieName, cookieValue, [expires])
18 # cookieName is the actual name of the cookie, as stored by the browser
19 # cookieValue is the value you want stored under that name
20 # [expires] optionally, is the time in seconds until the browser should expire the cookie
4774989 @irrelative edit
irrelative authored
21
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
22 ####Example
23 `web.setcookie()` can be used to set the cookie for a user, like this:
4774989 @irrelative edit
irrelative authored
24
25 class CookieSet:
26 def GET(self):
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
27 i = web.input(age='25')
28 web.setcookie('age', i.age, 3600)
29 return "Age set in your cookie"
30
4774989 @irrelative edit
irrelative authored
31
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
32 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
33
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
34 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 non-expiring cookie.
4774989 @irrelative edit
irrelative authored
35
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
36 ###Retrieving Cookies
37 ####Overview
38 There are two methods to retrieve cookies, depending on the desired reaction to a missing cookie.
39 #####Way 1 (raises exception if cookie is not found):
40 web.cookies().get(cookieName) # returns the value directly, but raises an exception
41 #cookieName is the name of the cookie submitted by the browser
42 #####Way 2 (avoids exception by setting default value for cookie if not found):
43 foo = web.cookies(cookieName=defaultValue)
44 foo.cookieName # return the value (which could be default)
45 #cookieName is the name of the cookie submitted by the browser
46
47 ####Example
48 `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
49
50 class CookieGet:
51 def GET(self):
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
52 c = web.cookies(age="25")
53 return "Your age is: " + c.age
54
55 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
56
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
57 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
58
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
59 class CookieGet:
60 def GET(self):
61 try:
62 return "Your age is: " + web.cookies().get('age')
63 except:
64 # Do whatever handling you need to, etc. here.
65 return "Cookie does not exist."
4074d92 @adamcrosby fixed text to match example, provided summary/prototype
adamcrosby authored
66
67
5c25034 @adamcrosby Hefty rewrite, clearing up some confusign variable names and adding s…
adamcrosby authored
68 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.
Something went wrong with that request. Please try again.