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>
--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.