Can someone explain cookies when used with CGI to me? I want to be able to retrieve a cookie using CGI.
Printable View
Can someone explain cookies when used with CGI to me? I want to be able to retrieve a cookie using CGI.
They are sent by HTTP Headers - the server sends a Set-Cookie: header and the Browser sends a Cookie: header. So they web server makes the cookie readable by your CGI script thru the HTTP_COOKIE environmental variable. Do a google search for "cookie " and your CGI language of choice for specific info on that language.
I can't find anything usefull. Everything I find is too complex (full cookie management).
I just want a basic example of how to read a cookie named user@dogs or whatever.
is this what you want
http://bignosebird.com/notebook/cookies.shtml
scoutt (or anybody else), I need a little help. I plugged in that code into a file named cookies.pl, and tried it out with this, however, the cookies never save. What am I doing wrong?
Sorry if the code is lengthy. Thanks for any help.Code:$adminpass = "bah";
&breakup;
&checklogin;
sub checklogin {
require "cookie.pl";
#read cookies
$cookie = &get_cookie("password");
if ($adminpass eq $cookie) {
print "login with cookie sucessful.";
} else {
if ($in{'action'} eq 'login') {
if ($in{'pass'} eq $adminpass) {
print "Content-type: text/html \n\n";
print "password correct";
$cookie = &set_cookie("password",$adminpass,0,"/","www.mindlessdrone.com");
print $cookie;
} else {
print "Content-type: text/html \n\n";
print "password incorrect";
}
} else {
print "Content-type: text/html \n\n";
print qq~
<html>
<form action="login.cgi" method="post">
<input type="hidden" name="action" value="login">
<input type="text" name="pass">
<input type="submit">
</form>
</html>
~;
}
}
}
#get input
sub breakup {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if ($buffer eq '') {
$buffer = $ENV{'QUERY_STRING'};
}
my @pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$in{$name} .= $value;
}
}
You need to "print" the Cookie with the HTTP headers, not as part of the HTML document - so before the two newlines that mark the end of the HTTP headers:
Code:print <<HTTPHEADERS;
Set-Cookie: username=josht&language=english
Content-type: text/html
HTTPHEADERS
I'll try this when I get home from school. One more quick question:
If my script is located in www.mindlessdrone.com/cgi-bin, should my code be:Code:&set_cookie("password",$adminpass,0,"/","www.mindlessdrone.com");
or...?Code:&set_cookie("password",$adminpass,0,"/cgi-bin/","www.mindlessdrone.com");
If the function works the way I think it works, the first snippet will cause the UA to return the cookie for any page in your site, where as the second snippet will cause the UA to return the cookie only for pages in or below the cgi-bin.
Oh! I understand now. So basically I was printing the context type before I was setting the cookie which made it so the cookie was never set.
I needed to change it to:
It all makes sense now. Thanks a million, Josh :)Code:$cookie = &set_cookie("password",$adminpass,0,"/","www.mindlessdrone.com");
print $cookie;
print "Content-type: text/html \n\n";
print "password correct";