PHP Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.21

HTTP Request Variables

Part:   1  2 

PHP Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Non ASCII Characters with MySQL

Inputting Non ASCII Characters

Controlling Response Header Lines

HTTP Request Variables

Sessions

Using Cookies

PHP SOAP Extension

PHP SOAP Extension - Server

Directories, Files and Images

Using MySQL with PHP

... Table of Contents

This chapter describes:

  • What are the predefined variables that store information from the HTTP request.
  • A sample script to test request variables.
  • How to promote request variables to stand alone variables.

Predefined Variables Related to HTTP Request

When PHP is used on a Web server to handle a HTTP request, it converts information submitted in the HTTP request as predefined variables:

  • $_GET - Associate array of variables submitted with GET method.
  • $_POST - Associate array of variables submitted with POST method.
  • $_COOKIE - Associate array of variables submitted as cookies.
  • $_REQUEST - Associate array of variables from $_GET, $_POST, and $_COOKIE.
  • $_SERVER - Associate array of all information from the server and the HTTP request.

Variables in those arrays can also be promoted (registered) as stand alone global variables using one of the following two ways:

  • Change "register_globals = on" in php.ini to register HTTP request variables on the entire server.
  • Call import_request_variables() to register HTTP request variables in one script only.

Here is my standard test program for HTTP request variables:

<?php # HttpRequestDetails.php
# Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
# 
   print "<pre>\n";
   print "\nContents of \$_GET:\n";
   foreach ($_GET as $k => $v) {
      print "   $k = $v\n";
   }
# 
   print "\nContents of \$_POST:\n";
   foreach ($_POST as $k => $v) {
      print "   $k = $v\n";
   }
# 
   print "\nContents of \$_COOKIE:\n";
   foreach ($_COOKIE as $k => $v) {
      print "   $k = $v\n";
   }
# 
   print "\nContents of \$_REQUEST:\n";
   foreach ($_REQUEST as $k => $v) {
      print "   $k = $v\n";
   }
# 
   print "\nContents of \$_SERVER:\n";
   foreach ($_SERVER as $k => $v) {
      print "   $k = $v\n";
   }
   print "</pre>\n";
?>

Run HttpRequestDetails.php at the command line, you will get:

<pre>

Contents of $_GET:

Contents of $_POST:

Contents of $_COOKIE:

Contents of $_REQUEST:

Contents of $_SERVER:
   CLIENTNAME = Console
   ComSpec = C:\WINDOWS\system32\cmd.exe
   HOMEDRIVE = C:
   PHPRC = c:\local\php
   PROCESSOR_ARCHITECTURE = x86
   SESSIONNAME = Console
   SystemDrive = C:
   SystemRoot = C:\WINDOWS
   PHP_SELF = HttpRequestDetails.php
   SCRIPT_NAME = HttpRequestDetails.php
   SCRIPT_FILENAME = HttpRequestDetails.php
   PATH_TRANSLATED = HttpRequestDetails.php
   DOCUMENT_ROOT =
   argv = Array
   argc = 1
   ......
</pre>

Not very interesting, right? $_GET, $_POST, $_COOKIE, and $_REQUEST are all empty.

Testing Request Variables - HttpRequestDetails.php

Now, let's run HttpRequestDetails.php on the local Web server. First copy HttpRequestDetails.php to \inetpub\wwwroot, then run Internet Explorer (IE) with http://localhost/HttpRequestDetails.php. You should get:

(Continued on next part...)

Part:   1  2 

Dr. Herong Yang, updated in 2006
PHP Tutorials - Herong's Tutorial Notes - HTTP Request Variables