Home / code / cgi

Some scripts

Environment variables

Perl has some special variables that allow you to request information. One of this special variables is %ENV. With %ENV we can call upon the so called environment variables. They give information over the system it serves on.

The %ENV variables are:

  • $ENV{REMOTE_ADDR}
  • $ENV{CONTENT_LENGTH}
  • $ENV{PATH_INFO}
  • $ENV{PATH_TRANSLATED}
  • $ENV{QUERY_STRING}
  • $ENV{REMOTE_HOST}
  • $ENV{REMOTE_USER}
  • $ENV{REQUEST_METHOD}
  • $ENV{SCRIPT_NAME}
  • $ENV{SERVER_NAME}
  • $ENV{SERVER_PORT}
  • $ENV{SERVER_PROTOCOL}
  • $ENV{SERVER_SOFTWARE}
  • $ENV{HTTP_USER_AGENT}
print "content-type: text/html\n\n";

print <<end;
<HTML>
<HEAD><TITLE>CGI-test</TITLE></HEAD>
<BODY>
end

print "Your IP-address is: $ENV{REMOTE_ADDR}\n";

print <<end;
</BODY>
</HTML>
end

A simple counter

A visit-counter is mostly a part of a website. Through a CGI script we ask how many times a page is requested.

In the next example the number of visitors is stored in the file counter.txt. We do the following:

  1. The text file is opened and is assigned to the filehandle DATA.
  2. The text file is read and assigned to the variable $counter.
  3. We increase the variable $teller with 1.
  4. We close the file and open it again to write the new value.
  5. We print the variable $teller.
  6. The text file is closed.

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

print <<end;
<HTML>
<HEAD><TITLE>Counter</TITLE></HEAD>
<BODY>
<H1>Counter</H1>
end

open (DATA, "counter.txt") || die "Cannot open counter file: $!\n";
  $counter=<DATA>;
  $counter++;
close (DATA);

open (DATA, ">counter.txt") || die "Cannot open counter file: $!\n";
  print DATA $counter;
close (DATA);

print "$counter visitors since 25-08-2000";

print <<end;
</BODY>
</HTML>
end

Showing send data

A visitors can send data through with a FORM. A CGI-script can then send it through mail or store it in a database.

To make this work there has to be a (HTML)page with the FORM and a CGI page. (Can both be put in 1 file).

Example:
<FORM action="scripts/password.plx" method="post">

<FORM action="mailto:webmaster@vdab.be" method="post">
All data is send in string-format (even numbers). The name and value are separated by a =, and the name/value pairs are separated by &. They are also encoded (URL encoded). This replaces spaces with + and other special values by a hexadecimal ASCII value.

The following example is a example of a CGI file.

read (STDIN, $data, $ENV{'CONTENT_LENGTH'});

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

print <<end
<HTML>
<HEAD><TITLE>Results</TITLE></HEAD>
<BODY>
end

print "$data";

print <<end
</BODY>
</HTML>
end

Working with send data

In the example we split the data-string we get from the FORM. First we separate the data by name/value (&). Then we separate the name and the values (=).

@pair = split(/&/, $data);

foreach $pair(@pair)
{
  ($name, $value)=split(/=/, $pair);
  $form{$name} = $value;
}

Now you have to remember that the value are still encoded. To replace the + back to spaces we use the following syntax: s/+/ /g;. The hexadecimal codes have all the form of %##, where 2 numbers (##) decide the character. For that we use a special syntax %([a-fA-F0-9] [a-FA-F0-9]).
Thus:

$value =~ s/+/ /g;
$value =~ s/%([a-fA-F0-9] [a-FA-F0-9])/pack("C",hex($1))/eg;

So if we put everything together :

read(STDI, $data, $ENV{'CONTENT_LENGTH'});
@pair = split(/&/, $data);

foreach $pair (@pair)
{
  ($name, $value)=split(/=/, $pair);
  $value =~ s/\+/ /g;
  $value =~ s/%([a-fA-F0-9] [a-FA-F0-9])/pack("C",hex($1))/eg;
  $form{$name} = $value;
}

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

print <<end;
<HTML>
<HEAD><TITLE>Thanks</TITLE></HEAD>
<BODY>
<H1>The following datahas been processed</H1>
<table width=80% border=1>
end

print "<TR><TD>Name:</TD><TD>$form{name}</TD></TR>";
print "<TR><TD>E-mail</TD><TD>$form{email};</TD></TR>";
print "<TR><TD>Comment</TD><TD>$form{comment}</TD></TR>";

print <<end;
</TABLE>
</BODY>
</HTML>
end

 

TOP

Latest script:

 

Books: