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

(Continued from previous part...)


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.

Part:   1  2 

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