home     documentation     download     Spyce logo

Documentation - Modules

Prev: 3.8.4 - Redirect Up: 3.8 - Modules Next: 3.8.6 - Session

3.8.5. Cookie

This module provides cookie functionality. Its methods are:

  • get( [key] ):
    Return a specific cookie string sent by the browser. If the optional cookie key is omitted, a dictionary of all cookies is returned. The cookie module may also be accessed as an associative array to achieve the same result as calling: namely, cookie['foo'] and cookie.get('foo') are equivalent.
  • set( key, value, [expire], [domain], [path], [secure] ):
    Sends a cookie to the browser. The cookie will be sent back on subsequent requests and can be retreived using the get function. The key and value parameters are required; the rest are optional. The expire parameter determines how long this cookie information will remain valid. It is specified in seconds from the current time. If expire is omitted, no expiration value will be provided along with the cookie header, meaning that the cookie will expire when the browser is closed. The domain and path parameters specify when the cookie will get sent; it will be restricted to certain document paths at certain domains, based on the cookie standard. If these are omitted, then path and/or domain information will not be sent in the cookie header. Lastly, the secure parameter, which defaults to false if omitted, determines whether the cookie information can be sent over an HTTP connection, or only via HTTPS.
  • delete( key ):
    Send a cookie delete header to the browser to delete the key cookie. The same may be achieved by: del cookie[key].
The example below shows to manage browser cookies.

[[.import name=cookie]]
  Managing cookies is simple. Use the following forms 
  to create and destroy cookies. Remember to refresh 
  once, because the cookie will only be transmitted on 
  the <i>following</i> request.<br>
  [[-- input forms --]]
  <form action="[[=request.uri('path')]]" method=post>
      <td align=right>Cookie name:</td>
      <td><input type=text name=name></td>
      <td align=right>value:</td>
      <td><input type=text name=value></td>
      <td>(required for set)</td>
      <td align=right>expiration:</td>
      <td><input type=text name=exp> seconds.</td>
      <td colspan=3>
        <input type=submit name=operation value=set>
        <input type=submit name=operation value=delete>
        <input type=submit name=operation value=refresh>
  [[-- show cookies --]]
  Cookies: [[=len(cookie.get().keys())]]<br>
    [[for c in cookie.get().keys(): {]]
    [[ } ]]
  [[-- set cookies --]]
    operation = request.post('operation')
    if operation:
      operation = operation[0]
      name = request.post('name')[0]
      value = request.post('value')[0]
      if operation == 'set' and name and value:
        cookie.set(name, value)
      if operation == 'delete' and name:
Run this code

Prev: 3.8.4 - Redirect Up: 3.8 - Modules Next: 3.8.6 - Session

Spyce logo
Python Server Pages
version 2.1.3
Spyce Powered SourceForge Logo