• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 28. Programming CGI Scripts > CGI Script Structure

CGI Script Structure

When your script is invoked by the server, the server passes information to the script via environment variables and, in the case of POST, via STDIN. GET and POST are the two most common request methods you'll encounter, and probably the only ones you'll need. (HEAD and PUT are also defined but seldom used for CGI.) The request method tells your script how it was invoked; based on that information, the script can decide how to act. The request method is passed to your script using the environment variable called, appropriately enough, REQUEST_METHOD.

  • GET is a request for data, the same method used for obtaining static documents. The GET method sends request information as parameters tacked onto the end of the URL. These parameters are passed to your CGI program in the environment variable QUERY_STRING.

    If your script is called myprog.exe, for example, and if you invoke it from a link with the form

    <A HREF ="cgi-bin/myprog.exe?lname=blow&fname=joe">

    the REQUEST_METHOD will be the string GET, and the QUERY_STRING will contain lname=blow&fname=joe.

    The question mark separates the name of the script from the beginning of the QUERY_STRING. On some servers the question mark is mandatory, even if no QUERY_STRING follows it. On other servers, a forward slash may be allowed instead of or in addition to the question mark. If the slash is used, the server passes the information to the script using the PATH_INFO variable instead of the QUERY_STRING variable.

  • A POST operation occurs when the browser sends data from a fill-in form to the server. With POST, the QUERY_STRING may or may not be blank, depending on your server.

    The data from a POSTed query gets passed from the server to the script using STDIN. Because STDIN is a stream and the script needs to know how much valid data is waiting, the server also supplies another variable, CONTENT_LENGTH, to indicate the size in bytes of the incoming data. The format for POSTed data is


    Your program must examine the REQUEST_METHOD environment variable to know whether to read STDIN. The CONTENT_LENGTH variable is typically useful only when the REQUEST_METHOD is POST.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint