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

Hi
I'm using perl to automate some testing on a web site.
I use the useragent, however once I have used it to log in to the site, it redirects to the logged in page then promptly seems to drop the cookies that get set to prove the login. Hence when the script follows a link on the page it goes straight to the login page again since logged in status has failed to verify.

Even explicitly setting and saving the cookies on each request, ie
        $ua->cookie_jar($cookies);
        my $content = $ua->request($req)->as_string;
        $cookies = $ua->cookie_jar();

seems to fail.

One possibility may be that when the cookies get first set, the page is followed by a redirect, around which I can't effectively grab and explicitly set the cookies. Is it likely that the cookies are being lost there ?

Anyone got any suggestions (it's fairly urgent now of course :)

Thanks

Specimen

Replies are listed 'Best First'.
Re: using cookies with UserAgent
by lhoward (Vicar) on Oct 03, 2000 at 15:56 UTC
    Try using the cookie jar like this:
    use LWP::UserAgent; use HTTP::Cookies; my $WWWAgent = LWP::UserAgent->new(); my $cookies=new HTTP::Cookies(file=>'./cookies.dat',autosave=>1); $WWWAgent->cookie_jar($cookies); ...
    with the HTTP::Cookies module. This hooks the cookies onto the LWP::UserAgent. So whatever you do with that LWP::UserAgent from then on will carry those cookies with it.
Re: using cookies with UserAgent
by Specimen (Acolyte) on Oct 03, 2000 at 15:54 UTC
    OK

    I discovered this independently (but thanks to kilinrax too).

    The problem is that i made the fatal (but very reasonable imho :) assumption that calling:
    $cookies = $ua->cookie_jar();
    
    after creation of the useragent ($ua) would return me a blank cookie for my future use. How naive. You need to explicitly create a blank HTTP::Cookie object and forcibly insert it into the useragent.

    I love perl but I keep getting burnt by things like this. Perhaps i should use strict after all :)

    Thanks

    Specimen
RE: using cookies with UserAgent
by Anonymous Monk on Oct 04, 2000 at 20:02 UTC
    Have you checked the path and domain attributes for your cookie ? Have you tried setting them using Javascript, does the same thing happen. Also try downloading something like Cookie Pal from tucows to alert you when you get a cookie, so you know it is being set.
redirecting directly after setting a cookie
by markjugg (Curate) on Oct 05, 2000 at 03:47 UTC
    I ran into the same problem using CGI.pm's cookie tricks. Here's what I discovered it (as I remember it): I was setting and retrieving the cookie correctly, the problem was that the cookie hadn't appeared in the environment before I tried to retrieve it, because the page that followed the redirect hadn't had it's headers sent yet, which contained the cookie. I saw a good solution to this in Justin Simoni's code (which I haven't tested yet.). He used the CGI.pm 'redirect' function, which can include attributes for both 'location' and 'cookie'. Before I found that, I was using a cheesy hack of printing a page with a meta tag refresh instead of redirecting directly. This insured that the cookie appeared in the environment before I tried to retrieve it again. Good luck! -mark
      There's nothing wrong with a good old refresh... just remember that the META tag is a way to insert data that should be treated as a header when you don't have the capability to insert a true header. Since you're writing CGI scripts, just add a real Refresh header that looks something like this:
      my $refresh_header = "Refresh: 0; URL=http://example.com/nextpage.cgi\ +n";
      This is perfectly correct and results in a new request with a particular delay. Unlike a traditional redirect, most browsers will *not* attempt to rePOST data (which they technically shouldn't do anyway, IIRC).

      That's not the problem the poster is having, though. He's writing a client.

      --isotope