Hudzilla.org - the homepage of Paul Hudson
Contents > HTML Forms > Handling data Wish List | Report Bug | About Me ]

7.4.1     register_globals

This is NOT the latest copy of this book; click here for the latest version.

Prior to PHP 4.1, variables submitted from external sources, e.g. session variables, cookies, form fields, etc, were automatically converted to variables inside PHP, as long as register_globals was enabled, which it was by default. These variables were also accessible through the arrays $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_SESSION_VARS, etc.

Imagine the following situation: you have a secure site, where members are identified by logon names, e.g. "Administrator", "Joe", "Peter", etc. The pages on this site track the username by way of the variable UserID, which is stored in a cookie on the computer when the user authenticates to the site.

With register_globals enabled, $UserID is available as a variable to all scripts on your site, which, whilst helpful, is quite a security hole. Take a look at the structure of this URL: http://www.yoursite.com/secure/forum.php?UserID=root

When register_globals is enabled, all variables sent by GET and POST are also converted to variables, and are indistinguishable from variables from other sources. The result of this is that a hacker could, by using the URL above, impersonate someone else - like root!

This was clearly a critical situation, and it was worryingly common. As such, the decision was taken to recommend all users to disable register_globals. In PHP 4.2, this was pushed further by having the default value of register_globals changed to "off", and this is how it has remained in PHP 5. Register_globals is not likely to ever be changed back to "on" for its default value, which means that it is best to learn the proper way of doing things: using the superglobals.





<< 7.4 Handling data   7.4.2 Working around register_globals: import_request_variables() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 07 Sep 2008

If register globals are set to on by a host, and I use the $_POST, etc. globals, is there any risk?

A PHP User - 07 Sep 2008

In addition to the previous poster, the $HTTP_ superglobal variables are deprecated as of PHP 5.

A PHP User - 07 Sep 2008

You should tell something about $_POST, $_GET and $_SESSION as well, since these are the new superglobal variables that should be used in the new PHP versions.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is two plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow