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;
print<<"EOF";
<script language="JavaScript">
document.cookie = 'sessionid=$sessionid';
</script>
EOF
$dbh->do("UPDATE logins set session_id='$sessionid',last=NOW() WHERE id='$userid'");
$V::goto="home";
}else{
$message="You have entered and incorrect Login and/or Password";
$V::goto="login";
}}
}

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

print out $ENV{HTTP_COOKIE} on the page some where.

what if thats not the only cookie?

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.

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

$ENV{HTTP_COOKIE}

I suppose you could do it like this:

#!/usr/bin/perl
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>";
}
else{
    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")
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.