i am writing a login script in a perl and i set a cookie. here it is:

if ($V::testsubmit eq 'yes') {
($userid)=$dbh->selectrow_array("SELECT id from logins WHERE username='$V::username' and password='$V::password'");
if ($userid > 0){
@alphanumeric = ('a'..'z', 'A'..'Z', 0..9);
$sessionid = join '', map $alphanumeric[rand @alphanumeric], 0..50;
<script language="JavaScript">
document.cookie = 'sessionid=$sessionid';
$dbh->do("UPDATE logins set session_id='$sessionid',last=NOW() WHERE id='$userid'");
$message="You have entered and incorrect Login and/or Password";

now how would i check if the cookie is set later on in the script, and if it is, output it?

what if thats not the only cookie?

If the name of the cookie you created is 'sessionid' you can read the value of the cookie into a variable as follows:

use CGI qw(:standard -debug);
use CGI::Carp qw(fatalsToBrowser);
#Create a new CGI object
my $cgi = new CGI;
my $value=$cgi->cookie('sessionid');

If no cookie named 'sessionid' exists, your $value variable will have no value assigned to it.

Edited 5 Years Ago by d5e5: What if cookie by that name not found?

is here a way to write a loop that will get the cookie using


I suppose you could do it like this:

use strict;
use warnings;
use 5.008;

use CGI qw(:standard);

my $name_to_search = 'sessionid';

my ($n, $v) = find_cookie($name_to_search);

print header();

if ($n eq 'NOTFOUND'){
    print "Cookie named '$name_to_search' not found</br>";
    print "Found cookie named '$n', Value is '$v'</br>";

sub find_cookie{
    my $name = shift;
    my $c = $ENV{HTTP_COOKIE};
    my @arr = split(/; /, $c);
    foreach (@arr){
        my @nv = split(/=/);
        next if $nv[0] ne $name;
        return @nv;
    return ('NOTFOUND',"$name cookie not found")
This article has been dead for over six months. Start a new discussion instead.