Home > Articles > Web Development > Dreamweaver & Flash

  • Print
  • + Share This

Stage 3: CGI—What Is It, and How Does It Work?

I am not the one to discuss this topic, so I have called in a friend to help out in this area. The following excerpt is from Michael Keszkowski, a programmer I work with. He also created and maintains the Source Code Central Web site.

CGI is a server-based service that provides an extra layer of functionality to a Web page. CGI is not a programming language; rather, it is a set of rules that the Web server follows to return dynamic content back to the browser. The Web server will transform an ordinary, static HTML page into a page that is alive with interesting, interactive content based on the instructions presented to it in a Perl script. Perl is the most commonly used language for writing CGI scripts.

Well, that covers the introductory material, so let me just drop the code in here and make a few notes about what you should change and what to look for when troubleshooting.

#!/usr/local/bin/perl

####### This uses the CGI.pm module, which parses the input
#   (from both POST and GET methods)
#    and stores those values in an object called $query.
####### The easiest and most painless way!
use CGI;
$query = new CGI;

#######
#    Location of the sendmail program
#######
 $sendmail = '/usr/lib/sendmail';

#######
#    Variables from Flash form
#######
   $to = $query->param("to");
	$from = $query->param("from");
  $body = $query->param("body");
  $subject = $query->param("subject");

#######
#    Executes the sendmail
#######

if ($to ne ""){
  if (($to =~ /@/) && ($to =~ /./)) {
  $sent = "sent=true";
print "content-type: text/html\n\n";
  print "$sent";
      open(MAIL, "|$sendmail -oi -t") or die "Can't open pipe
      to $sendmail: $!\n";
    print MAIL "To: $to \n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject \n";
    print MAIL "$body";
    close(MAIL) or die "Can't close pipe to $sendmail: $!\n";
    $sent="yes";
  exit();
  }else{
  $sent = "sent=invalid+email";
print "content-type: text/html\n\n";
  print "$sent";
  exit();
  }
}else{
  $sent = "sent=null+email";
print "content-type: text/html\n\n";
  print "$sent";
  exit();
}

This script is broken down into four commented sections. If you are not used to looking at Perl-CGI scripts, the commented areas are those lines preceded by the pound sign (#).

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.