perlbeginner
perlbeginner

Reputation: 33

Perl Cookie not working

I already spent all day looking for an answer for this:

I am using Perl with LWP::UserAgent and HTTP::Cookies.

My problem is that I can't get past an cookie-base age-check.

In Perl I use this code:

my $browser = LWP::UserAgent->new;
my $resp = $browser->get( $url, 'User-Agent' => 'MySpider/1.0' );

#Cookie Setup
my $cookies = HTTP::Cookies->new();
$cookies->set_cookie(1,'age_check', '1','/','.example.com/', 80, ,0,3354512128, 0);
$browser->cookie_jar($cookies);

The Site is setting the Cookie with JavaScript

function saveSplash(domain) {
    var expDate = new Date();
    expDate.setTime(expDate.getTime()+(1*24*3600*1000));
    setCookie("age_check", 1, expDate, '/', domain);
    setCookie("screen_width", getScreenWidth(), expDate, '/', domain);
}

This is the Cookie saved by my browser:

age_check
1
example.com/
1088
3354512128
30140182
2646218624
30139981

Any idea what I am doing wrong?

Thanks in advance guys!

Upvotes: 2

Views: 638

Answers (2)

Ashley
Ashley

Reputation: 4335

(Update: quite missed the point.)

I hope you didn’t really spend all day looking. :( The first result on Google for LWP::UserAgent JavaScript is Handling Javascript with LWP::UserAgent which gives the punchline: it doesn’t support JavaScript. There are a couple of options though.

Check Mechanize JavaScript on the CPAN. It leads to WWW::Mechanize::Firefox and WWW::Mechanize::Plugin::JavaScript. There is also scripting with WWW::Selenium which is a bit trickier but will perfectly emulate the browser because it really is running the browser.

Upate: forgot about WWW::Scripter which actually relates to Mech::Plugin::JavaScript.

Upvotes: 0

Anomie
Anomie

Reputation: 94834

I see two problems with your set_cookie call. First, the domain should be ".example.com" without a slash; the slash is specified in the path parameter. Second, you're missing a value for the path_spec parameter, so the value you specify for discard (0) is being used for maxage, which results in an expired cookie.

Upvotes: 1

Related Questions