PHP REGISTER_GLOBALS turned off? Hack It Back

For those of you that don’t know, REGISTER_GLOBALS is a PHP directive that when enabled automatically initialises variables with the values from forms, session, GET etc.

For example, the data a login form with the input fields named “username” and “password” will automatically be available as $username and $password.

REGISTER_GLOBALS is, in my opinion, bad practice and should be avoided - but this is a seperate debate.

If you are moving a web application or PHP is ugraded/changed and you have no control over the configuration of REGISTER_GLOBALS, the following hack will help:

foreach ($_GET as $key => $item)
{
$$key = $item;
}

This will loop through each GET value and assign it to a local variable. You can use this same logic for $_POST, $_SESSION etc. by replaceing $_GET in the foreach loop.

This code is intended for applications that need a temporary solution, are in a closed environment or are in testing. I do not recommend using REGISTER_GLOBALS or this hack in a public application.

Before PHP 4.2.0 REGISTER_GLOBALS was set to ON by default. Most hosts will now have REGISTER_GLOBALS disabled.

Are you a Developer, Designer or Copy Writer? Or Looking For One?

Click here to see 100s of jobs updated every day that you can complete to earn good money. Also find that programmer, designer or copy writer you have been looking for - advertise and get the best guy for your job - click here to find out more

3 Comments so far

  1. Olaf on January 3rd, 2007

    Hi,

    naming vars like this is a great and easy way to bypass the directive.

    But these kind of fixes have the result that people doesn’t program for modern web server (PHP5).

  2. Carey on January 3rd, 2007

    You are right that it is not good practice going forward - it should only be used as a temporary solution.

  3. SignpostMarv Martin on August 22nd, 2007

    Or, you know, you could extract($_GET);

    Much quicker.

Leave a Reply

Make $100,000/Month - Every Month!

It Works! We Make Our Customers Millionaires! Act now and get $600 bonus and a Free Money Making Website! - click to get started