Web Perl   «Prev  Next»

Display CGI variables - Exercise

Using the programs in the last two lessons as guides, write a CGI program that displays the user's hostname, IP address, and browser version.
#!/usr/bin/perl

$CRLF = "\x0d\x0a";
print "Content-type: text/plain$CRLF$CRLF";

foreach $varname (sort keys %ENV) {
  print "$varname: $ENV{$varname}\n"
}

#!/usr/bin/perl

$CRLF = "\x0d\x0a";
$browser = $ENV{"HTTP_USER_AGENT"};
$title = "What Browser?";

print "Content-type: text/html$CRLF$CRLF";
print <<BROWSER;
<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>

<p>You are using $browser. 
</body>
</html>
BROWSER

Test your program on your server to make sure it works.
If your server is publicly available to the Net, provide the URL of your program here, along with the source code.
If not, then the source code alone will be sufficient. Click the Submit button when you are finished.

If the hostname is not available on your server, it will display as the IP address.
In that case, your program will show the IP address twice. If that happens, it's OK.