#!/usr/bin/perl
foreach (split(/; /, $ENV{'HTTP_COOKIE'})) {
($chip,$value) = split(/=/);
$cookie{$chip} = $value;
}
print "$cookie{$ARGV[0]}"; |
 |
By using SSIs in your existing HTML document, you can retrieve the cookies and place them in your page when needed, as demonstrated in the example below. But before you do so, make sure that your server is configured to use SSIs. Also, check to see that the SSI uses the right path from the server's root, not the URL or document root:
<html>
<head><title>hello,
<!--#exec cmd="/real/path/to/cookie.pl name"-->
</title></head>
<body bgcolor="<!--#exec
cmd="/real/path/to/cookie.pl color"-->">
Welcome back,
<!--#exec cmd="/real/path/to/cookie.pl name"-->
<p>
I have never been to
<!--#exec cmd="/real/path/to/cookie.pl city"-->,
but I'd love to go there someday.
</body>
</html> |
 |
 |
This page is generated by retrieving the cookies that were set in the previous exercise. |
In the text above, the portion of code that looks like an HTML comment is actually an SSI, which we use to send a prebuilt page to the user. When the page is requested, the server parses the document and executes the program called by the SSI--in this case, the cookie.pl script we created earlier. Since we've already appended the name of the cookie to the SSI, we can pass it to the script. Now, when a user loads this page, the CGI checks to see if they have our cookie. If they do, the user will be presented with a personalized page.
See the complete Perl code.
|