PDA

View Full Version : cookie problem


eagle_1010
11-24-2005, 04:38 PM
I am trying to create a cookie, however it does not seem to work.

I have a page which displays html, this code is in a method.
I also have a method which creates a cookie and both of these methods are called at the start of the perl script.

If i try to create a cookie, nothing happens - no html is displayed - nothing. If i comment out the code, which tries to create the cookie, everything works fine.

I have googled and found different codes to create and store a cookie but none of them worked. What am I doing wrong?


sub create_initial_cookie
{
my $cookie = $q->cookie (-name => 'ID', -value =>"0");
}

nkrgupta
11-26-2005, 05:47 AM
You may be having a problem with printing out the appropriate headers. The following code sets a cookie --

#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Cookie;

$q=new CGI;

$cookie= $q->cookie(-name=>'DName', -value=>'login_time', -expires=>'+72h', -path=>'/');

print "Set-Cookie: $cookie\n";

print "Content-type: text/html\n\n";

print "Cookie set Successfully";

exit;

Call it from a browser.

The following code retrieves the cookie and checks it for a value --

#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Cookie;

print "Content-type: text/html\n\n";

$q=new CGI;

$fetch= $q->cookie(-name=>'DName');
if ($fetch eq 'login_time')
{
print "OK..!!";
}
else
{
print "Sorry";
}

Naveen

mlseim
11-26-2005, 02:05 PM
Remember that you can't set the cookie and look for it in the same
web page view, meaning you set the cookie and have at least one
page refresh before you can check for the cookie. This is because
the cookie is set when the next header goes out.

If this becomes a problem, you can create a separate Perl script that
only sets the cookies and then redirects back to another script ... this
will cause it to send that extra header you need to set the cookie(s).

A simplistic login script:

#!/usr/bin/perl

use CGI ':standard';

####
# Get username and clientid from a form
# and check for password or whatever
# ... other Perl code here

$redirect = "memberarea.pl"; # Where to go after the cookie is set.
# then, that script can check for cookies.

# set two cookies in this example ... you could set 1 or more.

my $oreo1 = cookie( -NAME => 'username',
-VALUE => "$username", ## a name or userid for example.
-EXPIRES => '', # M for month, m for minute ... blank = expire when browser closes.
-DOMAIN => '.mysite.com'); ## Enter your domain here

my $oreo2 = cookie( -NAME => 'client',
-VALUE => "$client", ## or email, or whatever
-EXPIRES => '', # M for month, m for minute ... blank = expire when browser closes.
-DOMAIN => '.mysite.com'); ## Enter your domain here

print redirect( -URL => $redirect,
-COOKIE =>[$oreo1,$oreo2]);



... and ... reading the cookies in the other Perl scripts:

#!/usr/bin/perl

use CGI ':standard';

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

## the rest of the script ...




.