Back - Home

PERL/CGI Program example

Life Demo

Complete program code
#!/usr/bin/perl -w

use strict;
use CGI;
use Socket; # necessary for gethostbyaddr()

my $q;

sub get_hostname($)
{
my $ip_dotted = shift;
my $xhost="";
my $iaddr;
  $iaddr = inet_aton($ip_dotted);
  $xhost = gethostbyaddr($iaddr,AF_INET);
  if (defined $xhost)
    {
    return($xhost);
    }
  return("-");
}

sub perlmain($)
{
  $q = new CGI;
  print $q->header("text/html");
  print $q->start_html( -title => "Mirror", -bgcolor => "#ffffcc" );

  print $q->h2 ("That is your browsers mirror image in the internet");

  print $q->p( "remote_addr: " . $q->remote_addr );
  my $hostname = get_hostname($q->remote_addr);
  print $q->p( "remote_host: " . $hostname);
  print $q->p( "user_agent: " . $q->user_agent );

  print $q->end_html;
  return(0);
}

my $globex = perlmain(1);
exit 0;