CGI Sampler (PHP, Perl, Python)

Purpose: To demonstrate how to run classic CGI scripts to render HTML from a back-end script. Do this exercise on localhost at a minimum, since we will be using CGI on localhost. You may also run it on the webhost, but that is optional.

A Little History

The HTML format was created by Timothy Berners-Lee in about 1990 to be a webpage format for the first internet browser, which he called "World Wide Web," and the first web server, which he called the CERN HTTP Daemon (httpd), which served up HTML or text content in response to a URL (it was a few years before httpd servers could serve up images and other content). There were no features in HTML to change the contents of the page or access the DOM tree like there are today. The general thought was -- if you want to adapt your HTML, run a script on the server to spit out the HTML. The idea of running remote scripts was one of the main reasons the internet was created in the first place, and the earliest versions of httpd allowed command-line scripts. The format for doing this was eventually called CGI (Common Gateway Interface). General purpose web-servers like Apache, Nginx, and LiteSpeed still use this standard, and it is helpful to know how it works.

How CGI Works

The idea of CGI is that the server runs a shell command, any shell command, so long as its output looks like this:

Content-Type: text/html

<HTML content starts here.>

Or it can deliver any other kind of content, so long as it looks like this:

Content-Type: image/jpeg

<Raw JPEG content starts here.>

This format is a minimal example of what HTTP responses look like. The top line is an HTTP header. Several headers can be used, each on its own line, but the "Content-Type" header is required. After the headers, there must be one blank line, followed by the content. If you are interested, you can see HTTP response headers for any page or resource you access on your Developer Tools > Network Tab.

Make Some CGI Files and Run Them

Please create the following files in your document root, which will run CGI scripts (you may copy and paste):

Perl Example
Perl scripts can be run as classic CGI. Your XAMPP Apache is configured by default to run .pl files and treat them as CGI.
test.pl (Typical Windows)
test_mac.pl (Typical Mac)

As needed, replace C:\xampp\perl\bin\perl.exe or /usr/bin/perl with the path of Perl on your machine. The shell command "where perl" can usually help you find out its location if you don't know.

Python Example
Perhaps the simplest way to get a general web server like Apache to run Python is to use classic CGI. Install Python if you haven't.
test.py (Typical Windows)
test.py (Typical Mac)

As with Perl, you need to specify where your Python executable is, and the command "where python" can help. To run on XAMPP, either (1) rename your file from test.py to test.cgi, or (2) modify httpd.conf to recognize the .py extension (see below).

PHP Example
PHP does not require the #! ("shebang") or Content-Type header, because XAMPP's pre-installed PHP module does this for you.
test.php

PHP is designed to be wrapped inside HTML, so just write HTML and put in <?php ?> tags, which are pre-processed so the Browser never sees them. In theory, you could call on classic CGI, but there is no need.

Run from the URL bar in your browser:

Run from the Command Line:

Optional: Make Your Own Kind of CGI

You can run CGI from anything that runs from the command line. In the early days of web programming, compiling C++ code and running it as CGI was highly popular. Feel free to try it with a batch or shell script, C++ code compiled into an executable, or any program you know how to make, so long as the output is a Content-Type header on the first line, an extra newline, then the content. Remember that the CGI runs on the Server, so it is the Server's operating system that must execute it.

Optional: How to Change Which File Extensions XAMPP Recognizes as CGI

Open your httpd.conf file by going to XAMPP and clicking "Config" to the right of "Apache" (you can also find this file in your installed XAMPP directories, something like <Main dir>/apache/conf/httpd.conf). You should find a line that looks something like this:

AddHandler cgi-script .cgi .pl .asp
(mine is on line 435)

This line means that whenever Apache is asked to serve up a page ending in .cgi, .pl, or .asp, it will expect the file to use CGI format. You can add .py or whatever file types you want if you have access to the server config files.

Optional: How to Change Default Index Filenames in XAMPP

In httpd.conf, you should also find a line that looks like this:

DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \ default.php default.pl default.cgi default.asp default.shtml default.html default.htm \ home.php home.pl home.cgi home.asp home.shtml home.html home.htm
(mine is on line 285)

This line means that if Apache gets a URL that points it at a directory instead of a file, it will look for these special filenames in the directory. The first one that it finds is the one it will serve up. You may notice that several filenames come before index.html. That means that if there is an index.html but there is also one of these special filenames, these special filenames have precedence. In each of our CGI examples, we will use one of these filenames. If you wish, you can modify this line to add index.py, app.py, or any other filename you would like.