<html>
<head><title>tell us about yourself</title></head>
<body>
Fill in this information about yourself
<p>
<form method="post" action="http://your.server.com/cgi-bin/personal.pl">
Name: <input type=text name="name" size=20><br>
Color: <select name="color">
<option value="red"> red
<option value="blue"> blue
<option value="green"> green
<option value="aqua"> aqua
<option value="gray"> gray
<option value="lime"> lime
<option value="maroon"> maroon
<option value="navy"> navy
<option value="olive"> olive
</select><br>
City: <input type="text" name="city" size="25"><br>
<input type=submit value="personalize">
</form>
</body></html>
|
 |
 |
The form generated by this HTML code is straightforward: the user enters their name, picks a color, and types in a city. |
Now we create the personal.pl script. Since we start off with a form, the script starts off pretty much the same as the email program:
#!/usr/bin/perl
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
local($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~
s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# strip off any possible SSI comment tags.
$value =~ s/<!--(.|\n)*-->//g;
$in{$name} = $value;
} |
 |
Now that we have information about the user, we want to store it in their browser's cookie file. That way, we can recall the data when they come back to our site. All we have to do is print the cookies as name-value pairs , and they will be automatically stored on the user's browser. We will also set an expiration date for the cookies:
print "Set-Cookie: name=$in{'name'};",
"expires=Wed, 31-Dec-1997 00:00:00 GMT; path=/";
print "\n";
print "Set-Cookie: color=$in{'color'};",
"expires=Wed, 31-Dec-1997 00:00:00 GMT; path=/";
print "\n";
print "Set-cookie: city=$in{'city'};",
"expires=Wed, 31-Dec-1997 00:00:00 GMT; path=/";
print "\n";
|
 |
 |
If your browser is set to warn you about cookies, running this script will pop up three dialog boxes. Each box represents one cookie: your name, the color, and your city. |
Now we want to send a page that employs the user's preferences back to the browser. We start by telling the browser that the rest of our output will be in HTML. Notice the two line breaks that signal the end of the header and the beginning of the body:
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>hello, $in{'name'}</title>";
print "</head><body bgcolor\=\"$in{'color'}\">\n";
print "Welcome, $in{'name'}!<p>\n";
print "You're using the ";
print "$ENV{'HTTP_USER_AGENT'} ";
print "browser to read this page.\n";
print "You live in $in{'city'}\n";
print "Notice that the background ";
print "is your favorite color\n";
print "</body></html>";
|
 |
The above code sends a dynamic page back to the browser. The info that the user submitted will determine the page's contents. The page also displays the name of the browser that the visitor is using, which is included in the information the server collects each time a page is requested.
The cookies we sent earlier are now stored in the user's Web browser. To learn how to read that cookie, take a look at the next step in our tutorial.
See the complete Perl code.
|