 |
| How to work with cookies? |  |
 |
 |
To put some light to the cookie darkness we recomend you reading this mini howtos.
Cookie, an unknown creature.
Unfortunately cookies aren't
products of a bakery but little strings saved on youre PC's harddisk
unless you delete them(or they validity expires). The informations saved on youre PC will be only transfered to the server they came from.
Cookies aren't computer programs and mustn't damage files on youre PC.
Here! Have a cookie.
To set a cookie at user's PC you have to transfer it before all other outputs in HTTP-Header.
Therefor just place a line with following structure before "content-type" line:
set-cookie: <NAME>=<VALUE>;
or
set-cookie: <NAME>=<VALUE>; expires=<RFC-DATE>; path=<PATH>;
Example:
...
CgiWriteLn('Set-Cookie: User=John Q. Public; expires=Sunday, 01-Dec-2099 12:00:00 GMT; path=/;')
CgiWriteLn('Set-Cookie: Birthday=02.01.1960; expires=Sunday, 01-Dec-2099 12:00:00 GMT; path=/;')
CgiWriteLn('content-type: text/html')
CgiWriteLn('')
// untill here it was all HTTP-Header the following part is the HTML page.
CgiWriteLn('<html>')
CgiWriteLn('<head><title>cookie-test</title></head>')
CgiWriteLn('<body>Cookie set</body>')
CgiWriteLn('</html>')
...
This example sets two ckookies "User" and "Birthday" and sets their
value to "John Q. Public" and "02.01.1960". The durability of this
cookies is respectable becouse their validity expires at the end of the
century. (see "expires").
I've got a cookie from you.
Reading out cookies with tdbengine is very simple. A call of the CGIGetParam() function suffices to access all (accessible) cookies. Therefor just transfer to CGIGetParam() the text "cookie." then the name of the wanted cookie, for example cookie.Birthday.
...
Var cUser : String = CGIGetParam("cookie.User")
CgiWriteLn('content-type: text/html')
CgiWriteLn('')
// untill here it was all HTTP-Header the following part is the HTML page.
CgiWriteLn('<html>')
CgiWriteLn('<head><title>cookie-test 2</title></head>')
CgiWriteLn('<body>Hallo '+cUser+'!<br> I know what you did last somer!</body>')
CgiWriteLn('</html>')
...
Eat the cookie!
Sometime it's possible
that a cookie set before suddenly isn't of use anymore. For example:
The user registers newly and gets a new session number. From user's
view nothing militates against the dispose of this old cookie.
Therefor
it'll the best thing if you set the date of expiry at one day in the
past.
This can be done as allready described in the first line of a HTTP output.
...
CgiWriteLn('Set-Cookie: User=John Q. Public; expires=Monday, 01-Jan-2000 12:00:00 GMT;')
CgiWriteLn('content-type: text/html')
CgiWriteLn('')
// untill here it was all HTTP-Header the following part is the HTML page.
CgiWriteLn('<html>')
CgiWriteLn('<head><title>cookie-test</title></head>')
CgiWriteLn('<body>Cookie User deleted</body>')
CgiWriteLn('</html>')
...
The variable "User" in this cookie has no more validity so it won't be transfered to the server.
Continuative links:
http://wp.netscape.com/newsref/std/cookie_spec.html
Author: Thomas Friebel <tf@tdb.de>
|