Version 5 of Cookies

Updated 2004-05-12 13:27:25

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:

Login

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

 set tok [::http::geturl http://mysite.net/register/user-login.tcl -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 http://mysite.net/some/restricted_page.html -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.2003:

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?


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