1. Problem

You wish to check if the user has any cookies set from previous interactions with your application.

2. Solution

To retrieve a cookie with a given name, use the HTTPRequest.get_cookie() method.

     login_time = request.get_cookie('login_time')

get_cookie() returns a string containing the cookie's value; if no such cookie exists, get_cookie() returns None. You can also specify a different default value:

    login_time = int(request.get_cookie('login_time', time.time()))

3. Discussion

See CookieSecurity for a discussion of security considerations related to cookies.


CategoryCookbook