Slide 3.3: HTML web user interface construction
Slide 3.5: File processing using C/C++
Home

How to Construct the Exercise I (Cont.)

  1. Calling a CGI Perl Script CGI (Common Gateway Interface) is a standard for running external programs from a World-Wide Web HTTP server. C/C++ instead of Perl programming is the focus of this course. So the CGI Perl script CGIDemo.pl is minimal and it performs the following two tasks:

    • The first part is to retrieve user's data from the Web and put it into variables by using the param function:
         $name = $query->param( 'name' );
      the variable $name contains “mon” from the web input:
       <input type="text" size="32" name="name" value="mon">
      For details, check Perl CGI web-related module.

    • The second part does the actual processing by calling C/C++ programs via the function system( ).

    ~wenchen/public_html/cgi-bin/351/week3/CGIDemo.pl
    
    #!/usr/bin/perl
    use CGI;
    $query = new CGI; 
    $act   = $query->param( 'act'  );
    $name  = $query->param( 'name' );
    
    if ( $act eq "Submit" ) {
      # Remove leading and trailing spaces.
      $name =~ s/^\s*(\S*)\s*$/$1/;
      # For security, remove some Unix metacharacters.
      $name =~ s/;|>|>>|<|\*|\?|\&|\|//g;
      # For concurrency control, exclusively lock a dummy file.
      open  ( fh, "> dummy.txt" );
      flock ( fh, 2 );
      system( "./CGIDemo '$name'" );
      close ( fh );
    } 
    elsif ( $act eq "Help" ) { 
      system( "/bin/cat Help.html" );
    }