How To Enable Register Globals in PHP Without Access to php.ini

By | April 26, 2008

Sometimes you need to use the variables passed to script avoiding usage of superglobal arrays. If you are on shared hosting, you won’t have access to your php.ini file and won’t be able to change register_globals setting. A simple function will help you to do this.

<?

/**
* function to emulate the register_globals setting in PHP
* for all of those diehard fans of possibly harmful PHP settings :-)
* @author Ruquay K Calloway
* @param string $order order in which to register the globals, e.g. ‘egpcs’ for default
*/
function register_globals($order = ‘egpcs’)
{
// define a subroutine
if(!function_exists(‘register_global_array’))
{
function register_global_array(array $superglobal)
{
foreach($superglobal as $varname => $value)
{
global $$varname;
$$varname = $value;
}
}
}

$order = explode(“rn”, trim(chunk_split($order, 1)));
foreach($order as $k)
{
switch(strtolower($k))
{
case ‘e’: register_global_array($_ENV); break;
case ‘g’: register_global_array($_GET); break;
case ‘p’: register_global_array($_POST); break;
case ‘c’: register_global_array($_COOKIE); break;
case ‘s’: register_global_array($_SERVER); break;
}
}
}
?>