05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Consider the following code:<br />

<br />

This code assumes that $superuser can be set to true only if check_privileges( )<br />

returns true.However, with register_globals enabled, it’s actually a simple matter<br />

to call the page as page.php?superuser=1 to get superuser privileges.<br />

There are three ways to solve this problem: initialize your variables, disable register_<br />

globals in the php.ini file, or customize the variables_order setting to prevent GET,<br />

POST, and cookie values from creating global variables.<br />

Initialize Variables<br />

Always initialize your variables.The superuser security hole in the previous example<br />

wouldn’t exist if the code had been written like this:<br />

<br />

If you set the error_reporting configuration option in php.ini to E_ALL, as discussed<br />

in Chapter 13, you will see a warning when your script uses a variable before it initializes<br />

it to some value.For example, the following script uses $a before setting it, so<br />

a warning is generated:<br />

<br />

<br />

Sample<br />

<br />

<br />

<br />

<br />

<br />

Warning: Undefined variable: a in /home/httpd/html/warnings.php on line 7<br />

Once your script is in a production environment, you should turn off public visibility<br />

of errors and warnings, as they can give a potential hacker insight into how your script<br />

works. The following php.ini directives are recommended for production systems:<br />

display_errors = Off<br />

log_errors = On<br />

error_log = /var/log/php_errors.log<br />

286 | Chapter 12: Security<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!