Updated 2013-11-28 23:32:10 by pooryorick

Added to link to all Cookies related pages. 2002-07-30 Acacio Cruz

Although I usually dislike cookies, here's an interesting post from news:comp.lang.tcl -- RS:
   Subject    Re: How to Request / Respond Cookie in TCL
       Date   Wed, 22 Mar 2000 15:11:59 -0600
       From   Jeff David <[email protected]_THISlucent.com>
 Organization Lucent Technologies

Q. I am new to AOLServer/TCL. Can any expert out there shed some light on how to read from and write to cookies. Thanks in advance.

A. NaviServer offers ns_getcookie[1] and ns_setcookie[2], built-in.

To read a cookie:
  set cookie_value [ns_getcookie name default]

(default is optional)

To write a cookie:
  ns_setcookie name value

Options available include: -secure, -scriptable, -domain, -path and -expires. See documentation for details.

A. I don't know about AOLServer in particular, but on an Apache server I do the following in my CGI scripts:

To read a cookie:

The cookie is stored in the HTTP_COOKIE environment variable (this might differ on AOLServer). You can access env(HTTP_COOKIE) to get to it. If it isn't there, look at [array names ::env] to see where it might be -- RS

To write a cookie:

Assume the cookie is in the variable $cookie and the timestring containing the expiration time is in $expiretime. The cookie needs to be set right after the Content-type declaration but before the second newline. Like this:
 puts "Content-type: text/html"
 if {[string length $cookie]} {
        puts "Set-Cookie: order=$cookie; expires=$expiretime"
 puts "\n"
 puts "<HTML>"


I highly suggest reading cookie documentation to fully understand what goes on. I believe I got mine from Netscape's web site.

(DKF: That's http://www.netscape.com/newsref/std/cookie_spec.html for those that are interested...) UK gone with the wind try http://curl.haxx.se/rfc/cookie_spec.html

Try RFC 2965 [3] for the official low down - PT

How do I read/write a cookie using Don Libes' cgi.tcl?

Read a cookie with cgi_import. Write with cgi_export. For example:
 cgi_import oreo

This leaves the Tcl variable 'oreo' with the value of the cookie by the same name.
 cgi_export oreo

...does the opposite - sets the cookie from the Tcl variable.

Pretty simple, eh?

In case you're wondering why cgi.tcl doesn't just automatically establish variables for all the incoming cookies, it's to protect your code from having its variables smashed by a cookie that you weren't aware of.

You can also read/write cookies to different variables than the actual names - but that's not very common - see the docs for more info.

There are also a bunch of options for handling other cookie-related stuff, such as expiration dates, domain names, etc. Again, see the doc for more info.