CGI

Running CGI scripts

If you want to provide interactive or dynamic information on your homepage, you can use CGI scripts. This CGI script will be executed whenever someone on the Net requests its URL.

We use Apache with suexec. Because of this, all your CGI scripts will be executed under your own user-ID. If you write insecure scripts, this could be (only) a problem for the data in your account!

A short (and secure) CGI Example

1. Create the file ~/public_html/cgi-bin/clock.pl:

#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "Hello visitor from $ENV{REMOTE_ADDR}<BR>";
print "It is now ".localtime(time)."\n"; 

Note you may first have to create the cgi-bin directory.

2. Make sure that you have assigned the right permissions to your CGI script:

chmod 744 ~/public_html/cgi-bin/clock.pl

chmod 755 ~/public_html/cgi-bin

3. Now you can use your first CGI program by calling the address https://people.ee.ethz.ch/~YOURLOGINNAME/cgi-bin/clock.pl

Some points to note when writing CGI programs:

Debugging Perl CGI scripts

If your CGI refuses to work you might want to put the following lines at the very start of your script. They will help to debug the beast as all error messages should be printed on your Web browser.

BEGIN{
  my ($HDO)=0;
  print "Content-Type: text/plain\n\n";
  umask 022;
  select(STDOUT); $| = 1;     # make unbuffered
  close (STDERR);
  open (STDERR ,">&STDOUT") || die "Can't dup stdout";
  map {print "$_: $ENV{$_}\n"} keys %ENV;
  print "\n\n";
}; 


CategoryWEBA

Web/Homepage/CGI (last edited 2018-10-11 11:24:16 by pmeier)