Perl CGI  «Prev  Next»

Lesson 10Setting and reading cookies
ObjectiveLearn how to set and read cookies.

Setting and Reading Cookies


$crlf = "\x0d\x0a";        
$cookiehead = "UserID=007";  

print "content-type: text/html$crlf";
print "set-cookie: $cookiehead$crlf";
print "$crlf";
...rest of program


Reading a cookie

$cookie = $ENV{HTTP_COOKIE};
print "<p>Here's a cookie: $cookie<br>\n" if $cookie;
It is valuable to keep in mind (especially when you are testing your code!) that you won't see the cookie come back from the browser until the next connection. It takes a whole round-trip (in other words, Connection A: send the cookie to the browser; Connection B: get the cookie from the browser) for the cookie to appear.

Set Perl Cookie - Quiz

Click the quiz link below for another exciting and challenging (and brief) multiple choice quiz on cookies.
Set Perl Cookie - Quiz