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.

code, configuration files, log files, and other data outside that directory (e.g., in /usr/<br />

local/lib/myapp).This doesn’t prevent other users on the web server from accessing<br />

those files (see the section on “File Permissions” earlier in this chapter), but it does<br />

prevent the files from being downloaded by remote users.<br />

If you must store these auxiliary files in your document root, you can configure the<br />

web server to deny requests for those files.For example, this tells Apache to deny<br />

requests for any file with a .inc extension, a common extension for <strong>PHP</strong> include files:<br />

<br />

Order allow,deny<br />

Deny from all<br />

<br />

If you store code libraries in a different directory from the <strong>PHP</strong> pages that use them,<br />

you’ll need to tell <strong>PHP</strong> where the libraries are.Either give a path to the code in each<br />

include( ) or require( ), or change include_path in php.ini:<br />

include_path = ".:/usr/local/php:/usr/local/lib/myapp";<br />

<strong>PHP</strong> Code<br />

With the eval( ) function, <strong>PHP</strong> allows a script to execute arbitrary <strong>PHP</strong> code.<br />

Although it can be useful in a few limited cases, allowing any user-supplied data to<br />

go into an eval( ) call is asking to be hacked.For instance, the following code is a<br />

security nightmare:<br />

<br />

<br />

Here are the keys...<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

This page takes some arbitrary <strong>PHP</strong> code from a form and runs it as part of the<br />

script.The running code has access to all of the global variables for the script and<br />

runs with the same privileges as the script running the code.It’s not hard to see why<br />

this is a problem—type this into the form:<br />

include('/etc/passwd');<br />

Unfortunately, there’s no easy way to ensure that a script like this can ever be secure.<br />

294 | 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!