6.7. Cookies

Cookies management uses two built-in variables, COOKIE and SET_COOKIE

COOKIE is the cookie sent by the user agent to the server : if a piece of information was stored on the client side for the domain to which the script belongs, the script will receive this information. COOKIE is an instance of the class Cookie.SimpleCookie. The value of the cookie named foo is

val = COOKIE["foo"].value

If the script wants to store a cookie on the client side, it must use SET_COOKIE, which is also an instance of the class Cookie.SimpleCookie. For instance, to store the value of a cookie named foo and define the path where the cookie is valid :

SET_COOKIE["foo"] = "bar"
SET_COOKIE["foo"]["path"] = "/path/where/cookie/is/valid"

To define an expiration date for the cookie, you can use the function expire_format() in the built-in module k_utils, and pass an instance of datetime.datetime as argument :

import k_utils
import datetime
SET_COOKIE["foo"] = "bar"
SET_COOKIE["foo"]["path"] = "/path/where/cookie/is/valid"
# expire in 30 days
exp_date = datetime.datetime.now()+datetime.timedelta(30)
SET_COOKIE["foo"]["expires"] = k_utils.expire_format(exp_date)