PDA

View Full Version : Getting and setting a cookie


bcarl314
05-20-2003, 07:29 PM
All right, I'm trying to learn PERL (again) for a new job, and I need to use cookies (which I've never used) and can't get it to work.

I've already googled this for about 2 hours and really need someone to tell me whats wrong.

Here are the 2 files I have

file: cookie.cgi

#!c:\perl\bin\perl.exe -w


########################################
### Original: May 20, 2003 ###
### Author: Brandon Carlson ###
### Last Revised: May 20, 2003 ###
########################################



########################################
### Include Necessary Modules ###
########################################
use CGI qw/:standard/;
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);
require('cookie.lib');
########################################
### Begin Program ###
########################################
my $c = new CGI::Cookie(-name => 'foo',
-value => ['bar','baz'],
-expires => '+3M');
print "Set-Cookie: $c\n";
print "Content-Type: text/html\n\n";
print "<a href='page2.cgi'>See cookie</a>";


This seems to set the cookie properly.

file: page2.cgi

#!c:\perl\bin\perl.exe -w

########################################
### User Login Script ###
### This script is used to verify a ###
### users identity. It queries an ###
### oracle DB and if the user sup- ###
### lies the correct information, it ###
### will set a cookie on the user's ###
### computer to maintain a session. ###
### If the user information is in- ###
### valid, it will destroy the cook- ###
### ie. ###
########################################
### Original: May 20, 2003 ###
### Author: Brandon Carlson ###
### Last Revised: May 20, 2003 ###
########################################



########################################
### Include Necessary Modules ###
########################################
use DBI;
use CGI qw/:standard/;
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);

########################################
### Include Necessary File ###
########################################
require('subparseform.lib');
require('dbConn.lib');

########################################
### Begin Program ###
########################################

%cookies = fetch CGI::Cookie;
$cookie = $cookies{'foo'} -value;
print "Content-type: text/html\n\n";
print $cookie . "<br>";


All the screen prints out is a big fat "0"

I look at the source and all I see is:

0<br>


I know I'm missing something simple, can someone please help. Thanks

bcarl314
05-22-2003, 01:18 PM
I got this to work using the CGI interface
ie
$query = new CGI;
my $cookie = $query->cookie(-name=>"myName",-value=>"myVal");
print $query->header(-cookie=>$cookie);

but that is not my prefered method. Any help on the question above?

Thanks