Version 7 of Cookies

Updated 2004-05-12 15:30:08

A lot of webservers use client-side cookies for logging in. Here is some sample code of how to log in to an OpenACS 3.x server:


 package require http
 set login [::http::formatQuery email [email protected] password fooFoo!]

 set tok [::http::geturl -query $login]
 upvar \#0 $tok state
 set cookies [list]
 foreach {name value} $state(meta) {
     if { $name eq "Set-Cookie" } {
         lappend cookies [lindex [split $value {;}] 0]
 ::http::cleanup $tok

Using it

 set tok2 [::http::geturl -headers [list Cookie [join $cookies {;}]]]
 ... your code
 ::http::cleanup $tok2

This method will work for a lot of websites that ask for a username and password. For each site, you'll probably want to read the page source of the login page of that site to see where it POSTs to and what fields you need to supply. And you may need to play with your User-Agent headers too. A lot of sites actually restrict which browsers can login...

male 12.05.2004:

What is to do be done, if a website has already stored its cookie with login information and if I want to reuse the cookie to get a page via the http page?

I read the cookie file and to pass its data like mentioned above with the "-headers [list Cookie ..." option and I changed the useragent string to look like the Internet Explorer, but nothing worked.

Any hint or suggestion?

KPV For starters try using sockspy to see what Internet Explorer is sending and how it differs from what you're sending.

male 12.05.2004:

I used sockspy and found out, what part of the already existing data inside the cookies file had to be used. Now it works fine! Thanks!

This page also serves as a reference for all pages that deal with Cookie management by CGI processing scripts.

Also see Cookies: give and take