$x = $_GET["x"];
So PHP created the register_globals directive. When register_globals is on, the variable
$x is copied from the $_GET array and injected into the symbol table. The reasoning was, "Why make the programmer take the extra step?" And many PHP programmers thought this was just the way things worked. But more knowledgeable developers saw the danger in this feature, and it quickly became a lightning rod for the attentions of the PHP Security Project.
<?php
if (authorized()) $safe_client = TRUE;
if ($safe_client) /* DO SOMETHING ONLY SAFE CLIENTS CAN DO */
Now consider what happens when an unauthorized client presents a URL like this:
/path/to/script.php?safe_c<?php // RAY_unregister_globals.php?foo=bar
error_reporting(E_ALL);
// CALL THIS SCRIPT WITH A GET ARGUMENT LIKE THIS:
// ...php?foo=bar
// THE DESIRED OUTPUT IS SOMETHING LIKE THIS...
// REGISTER GLOBALS IS OFF
// GET: foo => bar
// Notice: Undefined variable: foo in /home/websitet/public_html/RAY_unregister_globals.php on line 24
// NULL
// Notice: Undefined variable: foo in /home/websitet/public_html/RAY_unregister_globals.php on line 30
// NULL
// IS IT ON?
if (ini_get('register_globals'))
{
echo "REGISTER GLOBALS IS ON";
}
else
{
echo "REGISTER GLOBALS IS OFF";
}
// SHOW THE GET ARGUMENTS
foreach ($_GET as $key => $value)
{
echo "
GET: $key => $value" . PHP_EOL;
var_dump($$key);
}
// UNREGISTER THE GLOBALS AND TEST TO SEE IF THE GET ARGUMENT IS STILL INJECTED
unregister_globals();
var_dump($$key);
// A FUNCTION TO SIMULATE REGISTER-GLOBALS OFF
function unregister_globals()
{
if (ini_get('register_globals'))
{
$array = array('_REQUEST', '_FILES');
foreach ($array as $value)
{
if(isset($GLOBALS[$value]))
{
foreach ($GLOBALS[$value] as $key => $var)
{
if (isset($GLOBALS[$key]) && $var === $GLOBALS[$key])
{
// ACTIVATE THIS FOR REAL-TIME OUTPUT
// echo 'FOUND ' . $key . ' = ' . $var . ' in $' . $value; . PHP_EOL;
unset($GLOBALS[$key]);
}
}
}
}
}
}
The desired output is something like this:
<?php
// ASSUME THAT PHP WILL INJECT $q INTO THE SYMBOL TABLE
if ($q == NULL) echo "MISSING $q= URL PARAMETER";
<?php
/**
* RAISE ERROR REPORTING LEVEL
*/
error_reporting(E_ALL);
/**
* THIS IS AN EXAMPLE OF AN UNDEFINED VARIABLE
* THE ORIGINAL PROGRAMMER ASSUMED THAT REGISTER GLOBALS
* WOULD INJECT A URL PARAMETER INTO THE SYMBOL TABLE
* AT $q BUT WITHOUT REGISTER GLOBALS THE INJECTION
* DOES NOT OCCUR AND MUST BE DONE MANUALLY
*/
if ($q == NULL) echo "MISSING $q= URL PARAMETER";
<?php
/**
* RAISE ERROR REPORTING LEVEL
*/
error_reporting(E_ALL);
/**
* THIS IS AN EXAMPLE OF REMEDIATED CODE USING THE TERNARY OPERATOR
* THE TERNARY OPERATOR ENSURES THAT $q IS CORRECTLY ASSIGNED
* EITHER FROM THE REQUEST VARIABLE IN THE URL PARAMETER OR
* TO THE PHP NULL VALUE.
*/
$q
= (!empty($_GET['q']))
? $_GET['q']
: NULL
;
/**
* NOW THE if() STATEMENT WILL WORK WITHOUT THROWING A "NOTICE"
*/
if ($q == NULL) echo "MISSING $q= URL PARAMETER";
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Is it a bad practice to initialize all of your variables with PHP?
I always initialize all of my variables to NULL or some preset value so I don't have any issues. I think it comes from VB coding.
Author
Commented: