Using Perl to get HTTP request headers

Before PHP and many new-age web scripting languages there was Perl. Today you can still use Perl for web development and it is included in xampp's web development suite.

In this post I will show you how to use the CGI module to get the HTTP request headers and then loop through them and print them out. This is handy if you want to know if a web visitor is using a proxy server etc.

The first line of code you need is the path to the Perl interpreter. In XAMPP it is:

#!C:\xampp\perl\bin\perl.exe

Next we include/use/set the CGI module, error reporting etc

use strict;
use warnings;
use CGI;

Next we call a new instance of the CGI class and map the headers array.

my $q = CGI->new;
my %headers = map { $_ => $q->http($_) } $q->http();

Finally we loop through the array and print out the key/value results.

print "Content-type:text/html\r\n\r\n";
print "Got the following headers:\n";
for my $header ( keys %headers ) {
    print "$header: $headers{$header}\n";
}

Comments

Popular posts from this blog

Overlay Version 2 is here with key events and iframe support

Free Pop-up / Overlay Script

Version 3 is here... new configuration