0

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?

3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by d5e5
0

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 by d5e5: What if cookie by that name not found?

0

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

$ENV{HTTP_COOKIE}
0

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")
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.