in reply to Stay logged in between requests with WWW::Mechanize
Assuming the remote server is using cookies rather than session IDs (you can find out by closing your browser and then trying to reconnect without logging in: if you can, then it's cookies; otherwise, it's sessions), you need to not only specify the cookie_jar that you're using but to also load it. I find that setting the cookie_jar and "autosave" in the call to 'new()' causes W::M to spit out errors - so I tend to do it manually. Sample script follows:
#!/usr/bin/perl -w use strict; use WWW::Mechanize; my $cookie_file = "/tmp/cookies"; my $agent = WWW::Mechanize->new(); if (! -s $cookie_file){ $agent->get("http://okopnik.com/PHP/other/cookies.php"); # Save the cookie from this session $agent->cookie_jar->save($cookie_file); } else { $agent->cookie_jar->load($cookie_file); $agent->get("http://okopnik.com/PHP/other/cookies.php"); } print $agent->content;
Assuming that you start with an empty "/tmp/cookies", this will populate it with a cookie the first time you run it (and show you a silly message that indicates that); subsequent runs will show that the cookie has been set and is active. Do note that the above PHP script has a fairly short cookie lifetime (2 minutes, if I recall correctly.)
ben@Tyr:/tmp$ ./cookie_test <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head><title>Cookies</title></head> <body> <p>This page comes with yummy cookies. It started at 1226629378, h +as been<br> reloaded 0 times, and has lasted 0 seconds.</p> </body> </html> ben@Tyr:/tmp$ ./cookie_test <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head><title>Cookies</title></head> <body> <p>This page comes with yummy cookies. It started at 1226629378, h +as been<br> reloaded 1 time, and has lasted 6 seconds.</p> </body> </html>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Stay logged in between requests with WWW::Mechanize
by Cody Pendant (Prior) on Nov 14, 2008 at 03:20 UTC | |
by sasdrtx (Friar) on Nov 14, 2008 at 03:48 UTC | |
by Cody Pendant (Prior) on Nov 14, 2008 at 05:33 UTC | |
by almut (Canon) on Nov 14, 2008 at 05:45 UTC | |
Re^2: Stay logged in between requests with WWW::Mechanize
by Anonymous Monk on Nov 14, 2008 at 02:47 UTC | |
by oko1 (Deacon) on Nov 15, 2008 at 13:29 UTC |