...

View Full Version : problem with perl/cgi and cookie



dark0s
03-01-2010, 01:48 PM
I want to set cookie in prova.html page.

prova.html page is:


<html>
<head>
<title>Prova</title>
<head>
<body>
<form action="prova.cgi" method="POST">
<fieldset>
<legend><h2>Set your cookie...</h2></legend>
<input type="text" name="name" /><br/>
<input type="text" name="value" /><br/>
<input type="text" name="expires" /><br/>
<input type="submit" name="submit" /><br/>
<fieldset>
</form>
<body>
</html>

prova.cgi script is:


#!/usr/bin/perl

use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $cgi = CGI->new();

print $cgi->header(-type => 'text/html');
print $cgi->start_html('Risposta a prova');

my $cookie = $cgi->cookie(-name => 'sessionID', -value => 'user@networkstore', -expires => '+1h');
my $mycookie = $cgi->cookie('sessionID');

print "<h2>Your cookie is $mycookie</h2>";

but cookie is not set.

Where is the problem?

savio

FishMonger
03-01-2010, 04:11 PM
You need to send the cookie in the header.

http://search.cpan.org/~lds/CGI.pm-3.49/lib/CGI.pm#HTTP_COOKIES

dark0s
03-01-2010, 05:15 PM
Ok, I modified my code:


#!/usr/bin/perl

use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $cgi = CGI->new();

print $cgi->header(-type => 'text/html');
print $cgi->start_html('Risposta a prova');

my $cookie = $cgi->cookie(-name => 'sessionID', -value => 'user@networkstore', -expires => '+1h');
print $cgi->header(-cookie => $cookie);
my $mycookie = $cgi->cookie('sessionID');

print "<h2>Your cookie is $mycookie</h2>";

now I can I set correctly $mycookie?

FishMonger
03-01-2010, 05:24 PM
Yes, that line is correct, but you need to pass $cookie to the header.

Also, you can't send and retrieve the same cookie at the same time.


#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $cgi = CGI->new();
my $cookie = $cgi->cookie(-name => 'sessionID',
-value => 'user@networkstore',
-expires => '+1h');

print $cgi->header(-type => 'text/html',
-cookie => $cookie);

print $cgi->start_html('Risposta a prova');

dark0s
03-01-2010, 05:35 PM
How can I retreive $mycookie values?

FishMonger
03-01-2010, 05:52 PM
my $mycookie = $cgi->cookie('sessionID');

That will retrieve the cookie, however, not in the same script execution where the cookie is being created and sent. Once it's been sent, you will be able to retrieve it in subsequent calls to your script(s).

dark0s
03-01-2010, 05:57 PM
How can I retreive $mycookie in correctly way.
What is the exact code.

FishMonger
03-01-2010, 08:06 PM
How can I retreive $mycookie in correctly way.
What is the exact code.


my $mycookie = $cgi->cookie('sessionID');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum