Gossamer Forum
Home : General : Perl Programming :

Cookie Problem

Quote Reply
Cookie Problem
I'm using this script to put up a cookie in my browser so that when a user logs in, it remembers the loginname of the person. I tested out this cookie script code

#!/usr/bin/perl

use CGI qw/:standard/;
# Create a CGI object.
my $IN = new CGI; # Define the cookie.
my $cookie = $IN->cookie( -name => 'ID', -value => 123456, -expires => '+1h', -domain => '' );
# Create the cookie.
print $IN->header( -cookie => $cookie );
# Fetch cookie.
my $id = $IN->cookie('ID');
print "Test: $id";


but my id always comes out with the value w, what is wrong?

wired
Quote Reply
Re: [wired_lain] Cookie Problem In reply to
well, for one thing you can't set a cookie and read it in the same process.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy thoughts] Cookie Problem In reply to
thanks for responding Andrew

I see hmmm

I figured out a code to it

but im wondering

I did it this way

sub GetCookies
{ @cookies = split(/;/, $ENV{'HTTP_COOKIE'});
$new= $cookies[1];
($name, $login) = split(/=/,$new);

}

I set a cookie for login

when I checked out my $ENV{'HTTP_COOKIE'}

it gave me this values

Folder=arai; name=nrieko; folder=%24f; ID=w

but I only put a cookie for name?

what are all the other values?

Last edited by:

wired_lain: Oct 28, 2002, 11:33 PM
Quote Reply
Re: [wired_lain] Cookie Problem In reply to
wellp'm...the other tags are from your server, i believe. they tell the browser what scripts can request that cookie. it wont generally send a cookie to a server if the requesting file isn't from a directory it allows access to.

as for the rest, try this for your get cookie sub:
sub getCookie {
my $cookie = $ENV{HTTP_COOKIE};
my @buffer = split(/; /, $cookie);

foreach $scalarbuffer (@buffer) {
my ($name, $value) = split(/=/, $scalarbuffer) {
$cookie{$name} = $value;
}

return %cookie;
}

if you want to get real fancy you could also add a safe-guard to traslate the urlified characters back from hex.
Then you just check and see if $cookie{userName} is equal to whatever. for my authentications i use an md5_base64 hash of the username and password plus a random number and then remove certain characters and use that for my cookie authentication.

anyhow, have fun!
Quote Reply
Re: [WolfMan2k] Cookie Problem In reply to
You could make life easier by using CGI.pm :)