#!/usr/bin/perl use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common;
#$myCookies = HTTP::Cookies->new( # file => "cookies.txt", # autosave => 1, # ); $myCookies = HTTP::Cookies->new();
$URL = "https://www.example.com/w/signup.php"; $UA = LWP::UserAgent->new(); $UA->cookie_jar( $myCookies );
# Find a particular cookie from a particular domain. Add 1 week to # it's expiration. Delete the original cookie, store the modified # cookie in our cookie jar. Uses an external namespace ($find::) to # get the key, path, and domain to search for. Sets $find::changed # to indicate the number of cookies that matched and were modified. sub addOneWeek { my ($version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $rest) = @_;
if( ($domain eq $find::domain) and ($path eq $find::path ) and ($key eq $find::key ) ) { $expires = $expires + (3600 * 24 * 7); # seconds per week $myCookies->clear( $domain, $path, $key ); $myCookies->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $rest ); $find::changed++; } }
# Find a particular cookie from a particular domain. Uses an external # namespace ($find::) to get the key, path, and domain to search for. Prints # all cookies that match. sub showCookies { my ($version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $rest) = @_;
if( ($domain eq $find::domain) and ($path eq $find::path ) and ($key eq $find::key ) ) { print "$domain, $path, $key, $val, $expires\n"; } }
# First fetch a web page that sends a cookie. $req = HTTP::Request->new( GET => $URL ); $resp = $UA->request($req);
$find::domain = "example.com"; $find::path = "/"; $find::key = "session_id";
# Show any matching cookies, in their original form. $myCookies->scan( \&showCookies );
# Find them, and bump their expiration time by a week. $myCookies->scan( \&addOneWeek );
# Show the cookie jar, now that we modified it. $myCookies->scan( \&showCookies );
|