Listing 6. homepage.pl

#!/usr/bin/perl -w
use strict;
use diagnostics;
use CGI;
use Mysql;

# Create an instance of CGI
my $query = new CGI;

# Get the "user_id" cookie value
my $user_id = $query->cookie("user_id");

# Stop unregistered users
if ($user_id == 0)
{
    print $query->header("text/html");
    print $query->start_html(-title =>
    "No entry");
    print "<P>Sorry, but you do not seem to be";
    print " a registered user.</P>\n";
    print $query->end_html;
    exit;
}

# Connect to the local database server
my $dbh = Mysql->connect("localhost", "test");

# Create our SQL command based on the form
# contents
my $command =
"select user_name,user_color from user_table ";
$command .= "where user_id = $user_id";

# Execute the command
my $sth = $dbh->query($command);
my $name = "";
my $color = "";
my @arr = ();

# Loop through the results, which should consist
# of one row (and assign $name and $color while
# we are at it)
while (@arr = $sth->fetchrow)
{
    ($name, $color) = @arr;
}
# Now return some text to the user
print $query->header("text/html");
print $query->start_html(-title => "About you");
print "<P>Your name is \"$name\".</P>\n";
print "<P>Your favorite color is ";
print "\"$color\".</P>\n";
print $query->end_html;