varnie
varnie

Reputation: 2595

Server doesn't see the cookies

What're the reasons that the server doesn't actually recognize the cookie I set using WWW::Mechanize in the code below?

#!/usr/bin/perl

use strict;
use warnings;
use HTTP::Cookies;
use WWW::Mechanize;

my $cookie_jar = HTTP::Cookies->new(
    file => "$ENV{'HOME'}/lwp_cookies.dat",
    autosave => 1,
    ignore_discard => 1
);

my $mech = WWW::Mechanize->new(
    agent => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13',
    cookie_jar => $cookie_jar
);

$mech->get("http://somesite.ru");

$cookie_jar->set_cookie(1, "__cookie", '1', "/", ".somesite.ru");

#the following line prints the cookie's data
print $cookie_jar->as_string, "\n";

$mech->post("http://somesite.ru/action.php",
    ['foo' => 'bar']);

As you can see the server runs php. If I put in that action.php script:

var_dump($_COOKIE); 

I'll get an empty array anyway... Thank you for all suggestions.

Upvotes: 1

Views: 113

Answers (1)

yasu
yasu

Reputation: 1364

Try this (period before domain name removed):

$cookie_jar->set_cookie(1, "__cookie", '1', "/", "somesite.ru");

Cookies for .example.com are sent to the server if you access www.example.com, somehost.example.com and so on, but not sent if you access example.com. This is because .example.com does not match example.com.

Upvotes: 2

Related Questions