Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
HTTP Request Variables (0 Comments)
Admin: Posted Date: April 4, 2010

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.

HTTP Request Variables 

This article 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.


Contents of $_GET:
Contents of $_POST:
Contents of $_COOKIE:
Contents of $_REQUEST:
Contents of $_SERVER:
CONTENT_LENGTH = 0
GATEWAY_INTERFACE = CGI/1.1
HTTP_ACCEPT = */*
HTTP_ACCEPT_LANGUAGE = en-us
HTTP_CONNECTION = Keep-Alive
HTTP_HOST = localhost
HTTP_ACCEPT_ENCODING = gzip, deflate
HTTPS = off
INSTANCE_ID = 1
LOCAL_ADDR = 127.0.0.1
NUMBER_OF_PROCESSORS = 1
OS = Windows_NT
ProgramFiles = C:\Program Files
REMOTE_ADDR = 127.0.0.1
REMOTE_HOST = 127.0.0.1
REQUEST_METHOD = GET
SCRIPT_NAME = /HttpRequestDetails.php
SERVER_NAME = localhost
SERVER_PORT = 80
SERVER_PORT_SECURE = 0
SERVER_PROTOCOL = HTTP/1.1
SystemDrive = C:
ORIG_PATH_INFO = /HttpRequestDetails.php
ORIG_SCRIPT_NAME = /HttpRequestDetails.php
DOCUMENT_ROOT = c:/inetpub/wwwroot
SCRIPT_FILENAME = c:\inetpub\wwwroot\HttpRequestDetails.php
PHP_SELF = /HttpRequestDetails.php
......

$_GET is still empty, because nothing is submitted in the HTTP request. Now try this URL: http://localhost/HttpRequestDetails.php?lang=PHP&search. You should get:

Contents of $_GET:
lang = PHP
search = 
Contents of $_POST:
Contents of $_COOKIE:
Contents of $_REQUEST:
lang = PHP
search = 
Contents of $_SERVER:
......
QUERY_STRING = lang=PHP&search
......

Registering HTTP Request Variables as Global Variables

As I mentioned in the first section, there are two ways to promote variables stored in the request as stand alone global variables:

To promote request variables for the entire server, edit \php\php.ini and set:

register_globals = on

To promote request variables for one script only, use the following function:

import_request_variables("GPC",$prefix);

where "GPC" indicates that all variables from GET, POST and COOKIE are prompted. $prefix defines a prefix string that are to be added to the variable names.

Here is a sample script, RequestVariables.php

<?php # RequestVariables.php
# Copyright (c) 2002 by Dr. Herong Yang
# 
print "<pre>\n";
print "\nContents of \$_REQUEST:\n";
foreach ($_REQUEST as $k => $v) {
print "   $k = $v\n";
}
# 
print "\nLocl imported variables from the request:\n";
import_request_variables("GPC","r_");
print "   \$r_lang = $r_lang\n";
print "   \$r_search = $r_search\n";
# 
print "\nGlobaly imported variables from the request:\n";
print "   \$lang = $lang\n";
print "   \$search = $search\n";
print "</pre>\n";
?>

Try the script with http://localhost/RequestVariables.php?lang=PHP&search, you will get:

Contents of $_REQUEST:
lang = PHP
search = 
Locl imported variables from the request:
$r_lang = PHP
$r_search = 
Globaly imported variables from the request:
$lang = PHP
$search = 

Conclusion

$_GET in PHP is similar to the request.QueryString object in ASP.

$_POST in PHP is similar to the request.Form object in ASP.

Promoting variables in the request to be stand alone variable makes it easy to use them. But it also increases the risk of colliding them with other variables used in the script.

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: