PHP: Working with Default Register Global Off

Long time ago I was facing a big problem when my webhosting upgrade their PHP version from PHP 4.x to PHP 5 which also change the default setting of Register Global from On to Off.

They didn’t even tell me if they’re upgrading their server. Suddenly I was hit by a big size error_log file and the file size was increasing significantly.

This was a big problem for me because all of my PHP script on my web were running under Register Global On. If you’re a programmer you should know what I mean. :-)

Hmm… as usual I opened my browser and clicked the amazing search engine Google. Many keyword I’ve tried to search for the solution and it took about 2 days until I get one that fits me.

Well, I forgot where the link is but this is the script that I use for my web until now.


<?php

/*
HOW TO USE:
   - save this file as globals.php or any name you like
   - include this file to all of the php files that designed in register_global=on environment
     usage ex. include("globals.php");
   - credits: Daniel Malau [ http://blog.malau.net ]
*/

$raw = phpversion();
list($v_Upper,$v_Major,$v_Minor) = explode(".",$raw); 

if (($v_Upper == 4 && $v_Major < 1) || $v_Upper < 4) {
    $_FILES = $HTTP_POST_FILES;
    $_ENV = $HTTP_ENV_VARS;
    $_GET = $HTTP_GET_VARS;
    $_POST = $HTTP_POST_VARS;
    $_COOKIE = $HTTP_COOKIE_VARS;
    $_SERVER = $HTTP_SERVER_VARS;
    $_SESSION = $HTTP_SESSION_VARS;
    $_FILES = $HTTP_POST_FILES;
}

if (!ini_get('register_globals')) {
    while(list($key,$value)=each($_FILES)) $GLOBALS[$key]=$value;
    while(list($key,$value)=each($_ENV)) $GLOBALS[$key]=$value;
    while(list($key,$value)=each($_GET)) $GLOBALS[$key]=$value;
    while(list($key,$value)=each($_POST)) $GLOBALS[$key]=$value;
    while(list($key,$value)=each($_COOKIE)) $GLOBALS[$key]=$value;
    while(list($key,$value)=each($_SERVER)) $GLOBALS[$key]=$value;
    while(list($key,$value)=@each($_SESSION)) $GLOBALS[$key]=$value;
    foreach($_FILES as $key => $value){
        $GLOBALS[$key]=$_FILES[$key]['tmp_name'];
        foreach($value as $ext => $value2){
            $key2 = $key . '_' . $ext;
            $GLOBALS[$key2] = $value2;
        }
    }
}
?>

Related posts

1 Comment so far

Leave a comment Subscribe to Comments RSS Trackback this entry

  1. July 12, 2007 3:09 pm

    I used some sort of autodownline script for my web. When I want to install to my new hosting, I face which same problem with you about the register global. Can you help me on how to make it?

Leave a Reply

:) :( :d :"> :(( \:d/ :x 8-| /:) :o :-? :-" :-w ;) [-( :)>- more »