25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Security ” 221<br />

Register Globals<br />

When set to On, the register_globals configuration directive automatically injects<br />

variables into scripts. That is, all variables from the query string, posted forms, session<br />

store, cookies, and so on are available in what appear to be locally-named variables.<br />

Thus, if variables are not initialized before use, it is possible for a malicious<br />

user to set script variables and compromise an application.<br />

Consider the following code used in an environment where register_globals is<br />

set to On. The $loggedin variable is not initialized, so a user for whom checkLogin()<br />

would fail can easily set $loggedin by passing loggedin=1 through the query string.<br />

In this way, anyone can gain access to a restricted portion of the site. To mitigate<br />

this risk, simply set $loggedin = FALSE at the top of the script or turn off<br />

register_globals, which is the preferred approach. While setting register_globals<br />

to Off is the preferred approach, it is a best practice to always initialize variables.<br />

if (checkLogin())<br />

{<br />

$loggedin = TRUE;<br />

}<br />

if ($loggedin)<br />

{<br />

// do stuff only for logged in users<br />

}<br />

Note that a by-product of having register_globals turned on is that it is impossible<br />

to determine the origin of input. In the previous example, a user could set $loggedin<br />

from the query string, a posted form, or a cookie. Nothing restricts the scope in<br />

which the user can set it, and nothing identifies the scope from which it comes. A<br />

best practice for maintainable and manageable code is to use the appropriate superglobal<br />

array for the location from which you expect the data to originate—$_GET,<br />

$_POST, or $_COOKIE. This accomplishes two things: first of all, you will know the origin<br />

of the data; in addition, users are forced to play by your rules when sending data<br />

to your application.<br />

Before <strong>PHP</strong> 4.2.0, the register_globals configuration directive was set to On by<br />

default. Since then, this directive has been set to Off by default; as of <strong>PHP</strong> 6, it will<br />

no longer exist.<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!