#!/usr/bin/env python """A cgi script that counts how many times the user has visited a web site. A quick example on how to use cookies. This script assumes that you have the Cookie module from Python 2.0. If not, you might need to fiddle with it a bit --- not quite sure. Author: Danny Yoo (dyoo@hkn.eecs.berkeley.edu) """ from Cookie import SimpleCookie import cgi import os def getCookie(initialvalues = {}): """Return a SimpleCookie. If some of the cookie values haven't been set, we'll plunk them into the cookie with the initialValues dict.""" if os.environ.has_key('HTTP_COOKIE'): C = SimpleCookie(os.environ['HTTP_COOKIE']) else: C = SimpleCookie() for key in initialvalues.keys(): if not C.has_key(key): C[key] = initialvalues[key] return C if __name__ == '__main__': cookie = getCookie({'counter': 0}) cookie['counter'] = int(cookie['counter'].value) + 1 print cookie print "content-type: text/plain\n\n" print "Here's our count:", cookie['counter'].value