| You are not logged in Either you are not a registered user, or your previous session has timed out. |
|||||||
| Home | Jokes | Media | Photo | Travel | Community | Programs | Resume |
| Programs Submenu: Tcl Tips | Projects  | |||||||
This is one of the main focuses of my programming these days. Until a few months ago I was using Apache web server, with Tcl as my CGI language. Recently I've started using AOLServer, which includes native support for Tcl, which makes it much more efficient. It also allows for Tcl code to be embedded into HTML pages. I won't get into that much here. I'll try to include code for both here.
Cookies are a way for you, the web programmer, to store little bits of information on the users computer. When the user views your CGI, you have the opportunity to send these cookies, and later you can retrieve them. Since the web is generally a "stateless" programming paradigm, whenever a user clicks something, or enters a page, you have little or no information on where he was, or what he was doing, before. If he comes back tomorrow, you have no way of identifying him, etc. Cookies change all that by letting you set "persistant" data in his web browser that you can use to "remember" things about him. For a detailed explanations of cookies and their attributes, see the netscape cookies page.
I have attached some procedures you can use to form/set/get cookies using either typical CGIs scripts, or AOLServer. Both files have the same procedures in them, although the actual guts of the procedures are different in the two cases (although semantically very similar).
The procedures provided are:
This forms a cookie header and sends it to the browser. In either case (AOL or CGI) you must do this BEFORE you return the page headers. For CGI scripts, this must be before the "Content-type text/html". For AOL scripts, this must be followed by a ns_return of some type (ns_return, ns_returnredirect, etc).
The required arguments are the name and value of the cookie. You can also specify the expire time (in days) which defaults to 1000, and need not be specified. Other optional paramters include path, which has a default of "/". The path determines which CGI scripts can retrieve the cookie. "/" is all CGI scripts. If I set path to "/~rbrooks", only CGI scripts under "/~rbrooks" could retrieve the cookie. This is only really useful if you share the web server with a lot of different web programmers and don't want them reading your users cookies. The last option is "secure". It is 0 by default (insecure) and if set to 1, will only send cookies in https. You'll have to look elsewhere to learn about https. If you need it, you'll know.
This takes the same arguments as SendCookie, but returns a string that contains the cookie header instead of sending this string to the browser. This is actually used internally by SendCookie but you might find some other use for it also.
This retrieves the value of a cookie, if it exists in the users browser. If not, it returns "NOCOOKIE".
You may want to remove a cookie at some time. To do so, just send the cookie again (with any value you want) and an expire_days that is negative. That way, the cookie will not be readable (the browser won't send you a cookie that is expired).
Here are some sample programs. I've tested them using CGI and AOLServer Tclpages. Let me know how they work for you.
For CGI
For AOLServer
| This document last modified: Friday, November 05, 2004 | me@rustybrooks.com |