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.

These directives ensure that <strong>PHP</strong> error messages are never shown directly on your<br />

web pages. Instead, they are logged to the specified file.<br />

Set variables_order<br />

The default <strong>PHP</strong> configuration automatically creates global variables from the environment,<br />

cookies, server information, and GET and POST parameters.The<br />

variables_order directive in php.ini controls the order and presence of these variables.The<br />

default value is "EGPCS", meaning that first the environment is turned into<br />

global variables, then GET parameters, then POST parameters, then cookies, then<br />

server information.<br />

Allowing GET requests, POST requests, and cookies from the browser to create arbitrary<br />

global variables in your program is dangerous.A reasonable security precaution<br />

is to set variables_order to "ES":<br />

variables_order = "ES"<br />

You can access form parameters and cookie values via the $_REQUEST, $_GET, $_POST,<br />

and $_COOKIE arrays, as we discussed in Chapter 7.<br />

For maximum safety, you can disable register_globals in your php.ini file to prevent<br />

any global variables from being created.However, changing register_globals<br />

or variables_order will break scripts that were written with the expectation that<br />

form parameters would be accessible as global variables.To fix this problem, add a<br />

section at the start of your code to copy the parameters into regular global variables:<br />

$name = $_REQUEST['name'];<br />

$age = $_REQUEST['age'];<br />

// ... and so on for all incoming form parameters<br />

Filenames<br />

It’s fairly easy to construct a filename that refers to something other than what you<br />

intended.For example, say you have a $username variable that contains the name the<br />

user wants to be called, which the user has specified through a form field.Now let’s<br />

say you want to store a welcome message for each user in the directory /usr/local/lib/<br />

greetings, so that you can output the message any time the user logs into your application.<br />

The code to print the current user’s greeting is:<br />

<br />

This seems harmless enough, but what if the user chose the username "../../../../<br />

etc/passwd"? The code to include the greeting now includes /etc/passwd instead.Relative<br />

paths are a common trick used by hackers against unsuspecting scripts.<br />

Another trap for the unwary programmer lies in the way that, by default, <strong>PHP</strong> can<br />

open remote files with the same functions that open local files.The fopen( ) function<br />

and anything that uses it (e.g., include( ) and require( )) can be passed an<br />

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

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Filenames | 287

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

Saved successfully!

Ooh no, something went wrong!