next up previous contents index
Next: Database Up: Cookies Previous: Cookies   Contents   Index

Creating Cookies

You can create a cookie very easily. Here is a program (cookie.cgi) that creates a cookie that counts how many times you have visited the page. First you need to know the name of your server. Replace SERVER with your hostname:

#!/usr/bin/perl

$cookie = $ENV{'HTTP_COOKIE'};
if ($cookie) {
    if ($cookie =~ /count=([^;]*)/) {$count = $1;}
}
if ($count) {
    $count++;
} else {
    $count = 1;
}

print "Content-type: text/html\n";
print "Set-Cookie: count=$count; path=/; domain=SERVER\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Cookie Counter</title>\n";
print "</head>\n";
print "<body bgcolor=ffffff>\n";

print "You have visited $count time(s).<br>\n";

print "</body>\n";
print "</html>\n";

exit;

You can then look at the page at http://SERVER/cgi-bin/cookie.cgi where SERVER is your computers hostname. Repeatedly pressing the Reload button should give you a different number each time. If the number is constantly 1 then there are two likely possibilities. Either you did not change the word SERVER to your server's hostname or the browser is not accepting cookies.



Joseph Colton 2002-09-24