954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

cookie

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?

tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
 

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

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

what if thats not the only cookie?

tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
 
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.

d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

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

$ENV{HTTP_COOKIE}
tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
 

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")
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: