To implement this add a form and hidden tags to the first CGI script, such as
print "<input type='hidden' name='hiddenname' value='",name,"'>"
print "<input type='hidden' name='hiddencomments' value='",
comments,"'>"
The second CGI script retrieves these strings via
name = form.getvalue('hiddenname')
comments = form.getvalue('hiddencomments')
The following code in the first CGI script sets a cookie. Note that the cookie needs to be set before printing the Content-Type line but after reading the form values.
import Cookie
C = Cookie.SimpleCookie()
C["cookie_name"] = name
C["cookie_comments"] = comments
print C
print "Content-Type: text/html\n"
The second CGI script retrieves the cookie:
import Cookie
import os
C = Cookie.SimpleCookie()
C.load(os.environ["HTTP_COOKIE"])
name = C["cookie_name"].value
comments = C["cookie_comments"].value
(Note: The example above refers to session cookies that are not
permanently stored on the computer. I could not get the code above
to work for cookies that are stored on the computer, mainly because
on a PC the newline character is "\r\n" instead of "\n" and because
stored cookies need an expiration time. Something like the
following should work:
import time
temp = time.strftime("%a, %d-%b-%Y ", time.gmtime())
hour = time.strftime("%H", time.gmtime())
hour = str(int(hour) + 1)
if len(hour) <2 : hour = "0" + hour
temp = temp + hour + time.strftime(":%M:%S GMT", time.gmtime())
cookiedata = name + "|" + comments
print "Set-Cookie: nameandcomments="+cookiedata+"; expires=" + temp +"\r"
print "Content-Type: text/html\r\n\r"
3.2 (Optional) Create yet another version of exercise 1.1 but this time save the information to a file.
There are several problems involved with saving to a file because several users can look at the CGI script at the same time. To keep track of which user wrote which comments, write user name and comments to the file; send the user name as hidden text and use the name to retrieve the correct comments from the file.