PDA

View Full Version : Cookies not saving


Username
12-11-2005, 01:58 AM
$cookie_jar = HTTP::Cookies::Mozilla->new(
file => $File,
autosave => 1
);

$req = new HTTP::Request GET => $url;
$cookie_jar->add_cookie_header($req);
$response = $browser->request($req);
$cookie_jar->extract_cookies($response);

I'm a total perl n00b so any help would be appriciated.

mlseim
12-11-2005, 03:08 AM
I'm not sure what your snippet of code does, but keep in
mind that you can't set a cookie and read it in the same
run of the script.

You have to set the cookie and do another refresh to read it.

Or, set the cookie with one script and call another to read it ...
or, set and read the cookie with the same script, with a header
transfer between them.

Same thing with Javascript ... If you set the cookie with Javascript,
you can read it with Perl.

It has to do with headers that get transfered ... which is when the
cookies get sent and also get read.

Here's how to set a cookie and redirect to another script (where the
cookie can be read).

#!/usr/bin/perl

use CGI ':standard';

$username="Bill_Smith";
$password="123456";

$oreo = cookie( -NAME => 'username',
-VALUE => "$username",
-EXPIRES => '', # M for month, m for minute
-DOMAIN => '.yoursite.com'); #<-- enter your domain here

$oreo2 = cookie( -NAME => 'client',
-VALUE => "$password",
-EXPIRES => '', # M for month, m for minute
-DOMAIN => '.yoursite.com'); #<-- enter your domain here

$redirect = "members_area.pl";
print redirect( -URL => $redirect,
-COOKIE =>[$oreo,$oreo2]);


... and this would be the script called "members_area.pl":

#!/usr/bin/perl

use CGI ':standard';

my $query = new CGI;
my $cookie_in = $query->cookie("username");
$username = $cookie_in;
my $cookie_in = $query->cookie("password");
$password = $cookie_in;

hyperbole
12-11-2005, 05:49 PM
You need to associate your cookis jar with a user agent

Something like


use LWP::UserAgent;
use HTTP::Cookies;

$cookie_jar = HTTP::Cookies->new();

$user_agent = new LWP::UserAgent;
$user_agent->cookie_jar($cookie_jar);

$request = new HTTP::Request GET => $url;
$response = $user_agent->request($request);



I haven't tried saving the cookies to a file. What I've shown above automatically saves the cookies in memory and sends them out with each request. However, I think if you add the arguments from your script (file => $filename, autosave => 1), it should save your cookies in the file as well as reading them and sending them without your having to do anything else.



.