QUERY_STRING - CGI Query String

This section describes what is QUERY_STRING and how to use to receive information from the HTTP request URL.

Query String: The remaining part of the requesting URL string immediately after the "?". Query strings are usually used to pass a list of variable and value pairs with "&" as the pair delimiter, and "=" as the variable and value separator.

In order to avoid confusion, some characters are encoded in query strings. " " is encoded into "+". "+", "&", "=", "%" and other special characters are encoded into a hex number prefixed with "%".

Here is a CGI program to parse query strings, CgiQuery.pl:

#- CgiQuery.pl
#- Copyright (c) 1996 by Dr. Herong Yang, http://www.herongyang.com/
#
   print "Content-Type: text/html\n\n";
   print "<html><body>\n";
   print "<b>Query String Data:</b><br/>\n";
   $query = $ENV{'QUERY_STRING'};
   @list = split( /\&/, $query);
   foreach (@list) {
      ($var, $val) = split(/=/);
      $val =~ s/\'//g;
      $val =~ s/\+/ /g;
      $val =~ s/%(\w\w)/sprintf("%c", hex($1))/ge;
      print($var, ' = ', $val, "<br/>\n");
   }
   print "</html></body>\n";

Try it with this URL:

http://localhost/cgi-bin/CgiQuery.pl?Exp=1+%2B+2&Res=3

you will get:

Query String Data:
Exp = 1 + 2
Res = 3

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

 Converting Perl Script to Executable Binary

 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)

 What Is Common Gateway Interface (CGI)?

 IIS Environment Variables

QUERY_STRING - CGI Query String

 Calculator.pl - CGI Application Example

 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

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version