rjahrman has asked for the wisdom of the Perl Monks concerning the following question:

Basically, I'm trying to write a program that will log in to a website using my username and password and record the cookie sent to me. It then needs to "echo" that cookie back to the server in a POST request.

I've done a few POST requests before using HTTP::Request::Common. However, never anything using cookies. I looked around and I think I need to use a "cookie jar." I was unable to find much info about this online. And I still don't understand what on earth a cookie jar is. Could someone post a code sample, or at least point me in the right direction?

Thanks!!!

update (broquaint): title change (was WTF is a Cookie Jar)

Replies are listed 'Best First'.
Re: What is a Cookie Jar?
by greenFox (Vicar) on Jul 04, 2003 at 07:11 UTC
    I would recommending looking at merlyn's Basic Cookie Management as a starting point.

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re: What is a Cookie Jar?
by antirice (Priest) on Jul 04, 2003 at 06:55 UTC

    Check out HTTP::Cookies for more information.

    For a general tutorial on using the LWP modules, check out the LWP cookbook.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: What is a Cookie Jar?
by Abstraction (Friar) on Jul 04, 2003 at 06:58 UTC
    From my understanding a cookie jar is just a place that holds all your cookie information. Someone may want to expand on this...

    As far as modules that will accomplish your task, I would recommed looking at WWW::Mechanize or it's super class LWP::UserAgent. Both will do what you want. If not, you'll have so share your problematic code.

Re: What is a Cookie Jar?
by bobn (Chaplain) on Jul 04, 2003 at 14:21 UTC

    cookie_jar is magic. When a user agent (e.g. your browser or your LWP::UserAgent program) is sent cookies, the site expects them echoed back in subsequent requests. However, a cookie can expire after a set amount of time, different sites will send different cookies - and expect only the ones they sent back. In fact different scripts at the same site will send different coookies and expect the right one sent back.

    cookie_jar automates all this, including possibly saving cookies to a file for reading on subsequent execution.

    # UNTESTED SNIPPET use LWP::UserAgent; $ua = new LWP::UserAgent; use HTTP::Cookies; $cookie_jar = HTTP::Cookies->new; $init_url = 'http://www.domain.com/cgi-bin/whatever.pl?X'; $request = new HTTP::Request('GET', $init_url); $cookie_jar->extract_cookies($response); # later.... $request = new HTTP::Request('GET', 'http://www.domain.com/cgi-bin/wha +tever.pl?Y'; $cookie_jar->add_cookie_header($request); $response = $ua->simple_request($request);
    Just extract from responses and add to requests - the cookie_jar does the heavy lifting.

    --Bob Niederman, http://bob-n.com
      Is there a reason you say "$request = new HTTP::Request('GET', $init_url);", but then on the next line the variable is $reponse instead of $request?

        Yes. I''m a space cadet.

        I was cutting and pating and forgot:

        $response = $ua->request($request);
        Also, after reading the lwpcookbook referenced in another post here, it appears I'm still doing more work than needed. Apparently, just attaching the cookie_jar to the UserAgent object does the job, thouhg I haven't actually tested this.

        update: I probably had to do the extract and add because I was using simple_request, due to doing other things (referer, other headers in redirected requests.) I expect that folks using the the normal $ua->request methind can just do a $ua->cookie_jar(options..) and forget about it.

        --Bob Niederman, http://bob-n.com
Re: What is a Cookie Jar?
by DigitalKitty (Parson) on Jul 04, 2003 at 17:09 UTC
    Hi rjahrman.

    A few years ago, Laura Lemay was a reasonably good author of computer books ( the 'Teach yourself' series ). She has since retired from that profession. Nevertheless, her boyfriend (an accomplished computer security expert/programmer) wrote a program you might be able to use. Here is the url:

    http://www.lne.com/ericm/cookie_jar/

    He also has a few other interesting programs available as well.

    Hope this helps,
    -Katie.