Browse files

edit

  • Loading branch information...
1 parent 6623b8a commit 47749896fce16b8cb70c01ad1ce817cf15244847 @irrelative irrelative committed with anandology Mar 1, 2008
Showing with 32 additions and 0 deletions.
  1. +32 −0 cookbook/cookies.md
View
32 cookbook/cookies.md
@@ -0,0 +1,32 @@
+---
+layout: default
+title: cookies
+---
+
+# cookies
+
+##Problem
+You want to set and retrieve cookies for a user browsing the site.
+
+##Solution
+
+web.setcookie can be used to set the cookie for a user, like this:
+
+
+ class CookieSet:
+ def GET(self):
+ i = web.input(name='Bob')
+ web.setcookie('name', i.name)
+ return "Name set in your cookie"
+
+Going to the above function will set a cookie with cookie name equal to "name" and cookie value default equal to "Bob", or whatever the user gives as input. You could set a custom name by going to /cookieset?name=Guido.
+
+
+web.getcookie can be used to access an already set cookie. If a cookie is set using the above code, it can be retrieved like this:
+
+ class CookieGet:
+ def GET(self):
+ c = web.cookies(name="Bob")
+ return "Your name is: " + c.name
+
+web.setcookie takes a third argument, "expires", so you can decide 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).

0 comments on commit 4774989

Please sign in to comment.