PHP Globals

In PHP, variables are automatically created the first time they are used (empty: "") in the code and their type is based on the content and context in which they are used. This makes variables easy to set and use for the developer.

PHP as a programming language that is most of the time used for web applications, often deals with user input.


  • Form variables
  • Cookies
  • Uploaded files

These take the input, process it and return the output. To handle the input easily, PHP provides a set of global variables.

Before PHP version 4.1.0 (including 4.1.0), all the variables (Environment (ENV), GET, POST, COOKIE, and SERVER) got written into the same namespace. So an attacker was able to put arbitrary variables with arbitrary values into the namespace. If the programmer doesn't initialize the variables in his code, they can't guarantee that the variable hasn't been changed by an attacker.


if ($pass == "noidea"
$auth 1
// Further logic here 
if ($auth == 1
"Access granted"

You can easily bypass the authentification by sending a variables "auth" to the script with the value 1.

After PHP 4.1.0 register_globals is turned OFF by default and variables get written into super global arrays.

If you call the script for example like so:


You could use variable1 and variable2 in your script, depending on the web server configuration, like this:

register_globals ON

//-->only possible if track_vars is on else:

register_globals OFF

If track_var is on you can use the following arrays:


track_vars enabled: variables submitted by the user are available both from the global variables and also as elements in the arrays mentioned above.

Sometimes you will have problems that a third-party PHP application needs register_globals ON! You can put for examples these lines

php_flag register_globals Off
php_flag track_vars On

into an .htaccess file to change the settings for only a special program. In the Apache web server configuration has "AllowOverride" be set to "AllowOverride Options"

Under the 'admin/' directory, index.php checks whether the password matches the one in the database after posting the form:

if ($dbpass == $pass) { 
header("Location: index2.php"); 


When the passwords match, the variables $myname, $fullname and $userid are registered as session variables. The user then gets redirected to index2.php. Let us see what happens there:

if (!$PHPSESSID) { 
header("Location: index.php"); 
} else { 
  if (!
  if (!
  if (!


If the session ID has not been set, the user will be redirected back to the login screen. If there is a session ID, though, the script will resume the session and will put the previously set session variables into the global scope. Nice. Let us see how we can exploit this. Consider the following URL:

The GET variables $PHPSESSID, $myname, $fullname and $userid are created as global variables per default. So when you look at the if-else structure above, you will notice that the script figures $PHPSESSID is set and that the three variables dedicated to authorize and identify the user can be set to anything you want. The database has not even been queried.

Tutorial by Raduce



Thanks again, it looks like you put a lot of effort into this one


Well this was a hard tut but after i finished exploit 5 from HackQuest,I thought this aspect deserves a tutorial.


very good tutorial. 5 from me :D