CGI Programming, the Basics

Oh No, Not This Again!

'Hello World' in C

#include <stdio.h>

int main( void )
{
  printf( "Content-type: text/html\n\n" );
  printf( "<HTML><HEAD><TITLE>Hello World</TITLE></HEAD>\n" );
  printf( "<BODY><H1>Hello World</H1></BODY></HTML>\n" );

  return 0;
}


'Hello World' in Perl

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Hello World</TITLE></HEAD>\n";
print "<BODY><H1>Hello World</H1></BODY></HTML>\n";

exit 0;


Example Output

Hello World Web Page


What Is There To Work With?

Env Program in C

#include <stdio.h>
#include <string.h>

int main( argc, argv, envp )
  int argc;
  char *argv[];
  char *envp[];
{
  char *var;

  printf( "Content-type: text/html\n\n" );

  printf( "<HTML><HEAD><TITLE>CGI Environmental Variables</TITLE></HEAD>\n" );
  printf( "<BODY><H1>CGI Environmental Variables</H1>\n<P>\n" );

  while( *envp != NULL )
  {
    var = strchr( *envp, '=' );
    *var++ = '\0';
    printf( "%s=<B>%s</B><BR>\n", *envp++, var );
  }

  printf( "</P>\n</BODY></HTML>" );

  return 0;
}

Env Program in Perl

#!/usr/local/bin/perl

sub PrintEnv
{
  foreach $key ( sort keys %ENV )
  {
    print "$key = <B>$ENV{$key}</B><BR>\n";
  }
}

print "Content-type: text/html\n\n";

print "<HTML><HEAD><TITLE>CGI Environmental Variables</TITLE></HEAD>\n";
print "<BODY><H1>CGI Environmental Variables</H1>\n<P>\n";

&PrintEnv;

print "</P>\n</BODY></HTML>";
exit( 0 );

Example Output

env.pl example output

Getting User Input


Last Modified: 17 February 1997

St. Louis Unix Users Group - Linux SIG