Wednesday, August 20, 2008

PHP Source Code for Manually Registering Global Variables

Methodology and Implementation


If you read up on the originial USENET thread, you know that there were a few suggestions made about going about this as well as posts pointing out why those solutions didn't quite fit. In order to get around some of the fallbacks and keep the original idea in tact, functions needed to be created.

By making use of PHP's superglobal arrays ($GLOBALS, $_SESSION, $_GET, $_POST, $_COOKIE, $_REQUEST, $_SERVER, $_FILES, $_ENV) and referencing variables, the solution became rather simple to accommodate the OP's idea.

You can read the thread to find out what brough on this interest if you are curious. What it comes down to is that the OP didn't want to trust register_globals, gpc_order and variables_order server settings to get it right. Therefore, the OP wanted a way to manually register the variables needed by pulling them from the appropriate place. This was to help the OP in creating more secure and stable PHP applications.

How this works is really quite simple. Say you have a variable named "var1" in a session. This variable would normally be accessed by $_SESSION['var1'] if register_globals is not turned on. That syntax creates a few extra keystrokes as well as more extra strokes when using heredoc syntax or quoted string output. However, if you were to be able to simply access that variable through $var1, that saves a lot of keystrokes when you start to use the variable over and over (as is likely the case with sessions and user input variables). For this to work correctly, however, if you were to set a value to the variable either through $var1 or $_SESSION['var1'], the compliment also needed to be changed. This is where variable references come in. (References are similar to pointers in the C programming language.)

The implementation is bery easy to use. In order to "register" or global $var1 variable, we'd simply call the correct function from below.

global_session('var1');

In doing so, we now are able to access the same value in memory by the following 3 methods:

  • $var1
  • $_SESSION['var1']
  • $GLOBALS['var1']

By setting a new value to any one of the above 3 variables will change the value for all of them.

Difference From register_globals = On

One difference I have spotted in the way these functions act from having register_globals enabled is in the case of session variables. If you had altered $var1 in a script (with PHP's register_globals = "On"), you'd have to manually add the new value back into the session. In most cases, these functions would make a more convenient method; however, there may be instances where you do not want to have the $_SESSION variable automatically updated.

Additional tests with other superglobal arrays reveal that the same behavior exists as above for all the superglobal arrays except $GLOBALS. Therefore, if you truly want to set global variable values globally, you would need to use the methods described on this page.

function global_get($var){
if(!
array_key_exists($var,$_GET))
$_GET[$var]='';
$GLOBALS[$var]=&$_GET[$var];
}

function
global_post($var){
if(!
array_key_exists($var,$_POST))
$_POST[$var]='';
$GLOBALS[$var]=&$_POST[$var];
}

function
global_session($var){
if(!
array_key_exists($var,$_SESSION))
$_SESSION[$var]='';
$GLOBALS[$var]=&$_SESSION[$var];
}

function
global_cookie($var){
if(!
array_key_exists($var,$_COOKIE))
$_COOKIE[$var]='';
$GLOBALS[$var]=&$_COOKIE[$var];
}

function
global_server($var){
if(!
array_key_exists($var,$_SERVER))
$_SERVER[$var]='';
$GLOBALS[$var]=&$_SERVER[$var];
}

function
global_files($var){
if(!
array_key_exists($var,$_FILES))
$_FILES[$var]='';
$GLOBALS[$var]=&$_FILES[$var];
}

function
global_env($var){
if(!
array_key_exists($var,$_ENV))
$_ENV[$var]='';
$GLOBALS[$var]=&$_ENV[$var];
}

function
global_request($var){
if(!
array_key_exists($var,$_REQUEST))
$_REQUEST[$var]='';
$GLOBALS[$var]=&$_REQUEST[$var];
}
?>

No comments: