Registering $_REQUEST Keys as Global Variables

This section provides a tutorial example on how to promote (or register) keys and values in the $_REQUEST array as global variables so that you don't have to use the array notation to access their values.

To access information stored in the $_REQUEST array, you can use the normal array element notation, $_REQUEST[$key]. For example, $_REQUEST["lang"] would return "PHP" in the example script used in the previous section.

But key-value pairs in $_REQUEST can also be promoted (or registered) as standalone global variables using one of two ways described below:

1. To register $_REQUEST key-value pairs as global variables on the entire server, edit \php\php.ini and set:

register_globals = on

With this setting, the PHP engine will automatically create a global variable for each key in $_REQUEST with the key name as the variable name. of course, the value associated with the key will be copied to the variable at the same time. For example, if $_REQUEST["lang"] contains "PHP", $lang will created with the value "PHP".

2. To register $_REQUEST key-value pairs as local variables for the current script execution only, use the following function:


where "GPC" indicates that key-value pairs copied from $_GET, $_POST and $_COOKIE will be registed as local variables. $prefix defines a prefix string that are to be added to variable names.

To test this, I turned on the register_globals setting in php.ini and wrote this sample script, RequestVariables.php:

<?php # RequestVariables.php
#- Copyright (c) 2015,, All Rights Reserved.
   print "<pre>\n";
   print "\nContents of \$_REQUEST:\n";
   foreach ($_REQUEST as $k => $v) {
      print "   $k = $v\n";
   print "\nLocal imported variables from the request:\n";
   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";

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

Contents of $_REQUEST:
   lang = PHP
   search = 

Local imported variables from the request:
   $r_lang = PHP
   $r_search = 

Globaly imported variables from the request:
   $lang = PHP
   $search = 

Last update: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

Retrieving Information from HTTP Requests

 Predefined Variables Related to HTTP Requests

 Operating System Information in $_SERVER

 Web Server Information in $_SERVER

 Information in $_GET and $_REQUEST

Registering $_REQUEST Keys as Global Variables

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials


 PDF Printing Version