Rishabh Kumar
Rishabh Kumar

Reputation: 33

Perl script not saving cookies on google chrome?

i am trying to learn to work with cookies using Perl. following is my code. but i dont know why the cookie is not getting saved in chrome. everytime i run this script a new cookie is created.

#!"C:\wamp\perl\bin\perl.exe" -w
print "Content-type: text/html\n\n";
use CGI::Carp qw( fatalsToBrowser );
use CGI;

my $q=new CGI;
$value=$q->cookie('lol');

$cookie=$q->cookie
(
-name=>'lol',
-value=>'gh',
-expires=>'+7d'
);

print $q->header(-cookie=>$cookie);

$q->start_html
(
-title=>'CGI.pm Cookies'
);

unless($value) {print "cookie is goint to set";}
else {print "Hi $value";}

$q->end_html;
exit;

Upvotes: 2

Views: 517

Answers (2)

user1154522
user1154522

Reputation:

Here's the output of your script:

Content-type: text/html

Set-Cookie: lol=gh; path=/; expires=Sat, 04-May-2013 11:16:12 GMT
Date: Sat, 27 Apr 2013 11:16:12 GMT
Content-Type: text/html; charset=ISO-8859-1

cookie is goint to set

You send the Content-Type response header twice: first, on line 2, and again on line 16 when you print $q->header(-cookie => $cookie).

In fact, the double newline on line 2 ends your HTTP headers. So the output of $q->header(-cookie => $cookie) will be treated as document body content, not as HTTP headers.

Quickest solution? Comment out line 2.

Upvotes: 2

gangabass
gangabass

Reputation: 10666

Your forgot to send your cookie to the client:

print header(-cookie=>$cookie);

Upvotes: 2

Related Questions