PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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 be 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:
import_request_variables("GPC",$prefix);
where "GPC" indicates that key-value pairs copied from $_GET, $_POST and $_COOKIE will be registered 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 2003 (c) HerongYang.com. 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"; import_request_variables("GPC","r_"); print " \$r_lang = $r_lang\n"; print " \$r_search = $r_search\n"; # print "\nGlobally 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 = Globally imported variables from the request: $lang = PHP $search =
Table of Contents
Introduction and Installation of PHP
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
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
►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
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS