Example

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).

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).

Retrieving Cookies

Overview

There are many methods to retrieve cookies, depending on the desired reaction to a missing cookie.

Way 1 (return None if cookie is not found):

web.cookies().get(cookieName)
#cookieName is the name of the cookie submitted by the browser

Way 2 (raises exception AttributeError if cookie is not found):

foo = web.cookies()
foo.cookieName

Way 3 (avoids exception by setting default value for cookie if not found):

foo = web.cookies(cookieName=defaultValue)
foo.cookieName # return the value (which could be default)
#cookieName is the name of the cookie submitted by the browser

Example

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:

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.

Sometimes, you want to know specifically if something doesn't exist, in which case you can use something like the following:

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.