SuperGlobal / register_globals

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steadythecourse
Forum Newbie
Posts: 5
Joined: Tue Oct 05, 2010 11:46 am

SuperGlobal / register_globals

Post by steadythecourse »

Hello All!
Can anyone explain why when I run the following code

Code: Select all

<?php
print_r($GLOBALS);
?> 
I get the following output

Array ( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) )


which is expected since register_globals is set to off and no variables are defined, but when I add the following code. which is just to echo out a variable from the superglobal $_SERVER, the print_r($GLOBALS) now outputs all the superglobal $_SERVER variables as if echo somehow made all the variables available to the script and now $GLOBALS sees them. maybe a stupid question I don't know. For obvious reasons I can't show the output, but it's easy enough to run the code to view your systems output.

Code: Select all

<?php
print_r($GLOBALS);
echo "<br />";
echo "<br />";
echo $_SERVER["DOCUMENT_ROOT"];
?> 

Thanks,
steadythecourse
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SuperGlobal / register_globals

Post by requinix »

PHP has a feature called auto_globals_jit. When enabled, $_SERVER and $_ENV won't be created unless your script tries to use them. This happens when PHP first compiles your code so as long as you have something involving $_SERVER or $_ENV they'll be created.

Since your first code didn't use $_SERVER, PHP never created it. Thus why it didn't show up.
In your second code you did so PHP created it, and it showed up.
steadythecourse
Forum Newbie
Posts: 5
Joined: Tue Oct 05, 2010 11:46 am

Re: SuperGlobal / register_globals

Post by steadythecourse »

Thanks for the quick response!
Post Reply