Retrieving Environment and Script Information

This section provides a tutorial example on how to retrieve information about the Web server environment and about the current CGI script file.

The next group of methods we want to test is for retrieving information from the Web server about the environment and the script file itself. Here is a simple example Perl CGI script: CGI-pm-Server-Info.pl:

#!c:/local/perl/bin/perl.exe
#- CGI-pm-Server-Info.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.

   use CGI;
   $query = CGI->new();

   $text = "";

   $text .= "Request method = ".$query->request_method()."\n";
   $text .= "Request URI = ".$query->url(-query=>1)."\n";

   $text .= "Path info = ".$query->path_info()."\n";
   $text .= "Path translated = ".$query->path_translated()."\n";
   $text .= "Remote host = ".$query->remote_host()."\n";
   $text .= "Remote addr = ".$query->remote_addr()."\n";
   $text .= "Script name = ".$query->script_name()."\n";
   $text .= "Referer = ".$query->referer()."\n";
   $text .= "Server name = ".$query->server_name()."\n";
   $text .= "Server port = ".$query->server_port()."\n";
   $text .= "Server software = ".$query->server_software()."\n";
   $text .= "Remote user = ".$query->remote_user()."\n";

   print $query->header();
   print $query->start_html(-title=>'CGI-pm-Server-Info.pl');
   print $query->pre($text);
   print $query->end_html();

Copy CGI-pm-Server-Info.pl to C:\local\apache\htdocs and browse to: http://localhost/CGI-pm-Server-Info.pl?name=joe&age=21&student

You should see the following result in the browser.

Request method = GET
Request URI =
   http://localhost/CGI-pm-Server-Info.pl?name=joe;age=21;student=
Path info =
Path translated =
Remote host = 127.0.0.1
Remote addr = 127.0.0.1
Script name = /CGI-pm-Server-Info.pl
Referer =
Server name = localhost
Server port = 80
Server software = Apache/2.2.25 (Win32)
Remote user =

Two interesting notes:

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

CGI.pm Module for Building Web Pages

 What Is CGI.pm?

 Generating HTML Document with CGI.pm

 Retrieving Query Parameters and Headers

Retrieving Environment and Script Information

 Redirecting Browser to a URI

 "Refresh" http-equiv Meta Tag

 What Is CGI::Cookie?

 Retrieving and Setting HTTP Cookies

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB