CGI environment variables

When a CGI script is called several environment variables are set and you can access these from Perl. To see what some of the environment variables are you can try the example perl CGI script at

This program simply accesses Perl's associative %ENV array to print out the values of the environment variables. You can have a look at the source of this program to see how it accesses the variables.

Possibly the most useful of these environment variables is the QUERY_STRING variable. In the above example this was the empty string, but if we append, say,

onto the end of the URL then the string after the ? gets put into the environment variable QUERY_STRING. Try opening the following URLs. The first one is the same as we used above.


CGI scripts allow HTML documents to be generated on the fly, and that means generating titles, headings, and anchors on the fly. This exercise is to produce an endless document which starts out on some numbered page (probably page 1) and has links to the previous and next pages. Every page is numbered and every page has links to the previous and next pages. Each page of the document is of course generated by the same CGI script, with only the QUERY_STRING variable differing each time.

The endless document is called initially with QUERY_STRING equal to 1, and anchors to the previous and next pages have their query string calculated from this. You may also like extend the use of QUERY_STRING so that instead of just containing the intended page number it also contains the value of the last page visited.

Previous Start Next