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

Hi, monks

I can't realize why my script don't want to save cookies to a file. Can you show me what's wrong?

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Cookies; my $cookie_jar = HTTP::Cookies->new; my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Ge +cko/20051111 Firefox/1.5"); $ua->cookie_jar( $cookie_jar ); my $KEYWORD_TOOL_URL = "https://adwords.google.com/select/main?cmd=Key +wordSandbox"; my $req = HTTP::Request->new(GET => $KEYWORD_TOOL_URL ); my $res = $ua->request($req); print $cookie_jar->as_string(); $cookie_jar->save("/var/www/html/images/cookie.dat");
And the output is:
Set-Cookie3: S="awfe=X4L006UNsxw:awfe-efe=X4L006UNsxw"; path="/"; doma +in=.google.com; path_spec; discard; version=0 Set-Cookie3: I="X/twsRQBAAA=.XKZGpa+G/d2H+Fw8FQr4PA==.8BjoI2BAzejIcK0I +vz/tOw=="; path="/select"; domain=adwords.google.com; path_spec; disc +ard; version=0
but at this point i have cookie.dat file with this content:
#LWP-Cookies-1.0
Why? Where is mistake?

Replies are listed 'Best First'.
Re: Cookies are not saved to disk
by atemon (Chaplain) on Aug 29, 2007 at 12:18 UTC

    Hi, this code works :)

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Cookies; my $cookie_jar = HTTP::Cookies->new(file=>"/var/www/html/images/cookie +.dat", autosave => 1, ignore_discard=>1); #added ignore_discard=>1 my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Ge +cko/20051111 Firefox/1.5"); $ua->cookie_jar( $cookie_jar ); my $KEYWORD_TOOL_URL = "https://adwords.google.com/select/main?cmd=Key +wordSandbox"; my $req = HTTP::Request->new(GET => $KEYWORD_TOOL_URL ); my $res = $ua->request($req); print $cookie_jar->as_string();
    the flag ignore_discard=>1 will make the cookie jar to save even cookies that are requested to be discarded. So its saved when ignore_discard=>1 means that your program has no bugs, but Google is sending the cookie with a request to discard.

    Monks, can anybody explain why Google is setting cookie & requesting to discard?

    Cheers !

    --VC



    There are three sides to any argument.....
    your side, my side and the right side.

      Server is asking the UserAgent (normally browsers ) to discard the cookie after current session. ie, its a session cookie :)
      Wow! It's working!!!! Thanks!