05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

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

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

Restrict Filesystem Access to a Specific Directory<br />

If your application must operate on the filesystem, you can set the open_basedir<br />

option to further secure the application by restricting access to a specific directory.If<br />

open_basedir is set in php.ini, <strong>PHP</strong> limits filesystem and I/O functions so that they<br />

can operate only within that directory or any of its subdirectories. For example:<br />

open_basedir = /some/path<br />

With this configuration in effect, the following function calls succeed:<br />

unlink("/some/path/unwanted.exe");<br />

include("/some/path/less/travelled.inc");<br />

But these generate runtime errors:<br />

$fp = fopen ("/some/other/file.exe", "r");<br />

$dp = opendir("/some/path/../other/file.exe");<br />

Of course, one web server can run many applications, and each application typically<br />

stores files in its own directory.You can configure open_basedir on a per-virtual host<br />

basis in your httpd.conf file like this:<br />

<br />

ServerName domainA.com<br />

DocumentRoot /web/sites/domainA<br />

php_admin_value open_basedir /web/sites/domainA<br />

<br />

Similarly, you can configure it per directory or per URL in httpd.conf:<br />

# by directory<br />

<br />

php_admin_value open_basedir /home/httpd/html/app1<br />

<br />

# by URL<br />

<br />

php_admin_value open_basedir /home/httpd/html/app2<br />

<br />

The open_basedir directory can be set only in the httpd.conf file, not in .htaccess files,<br />

and you must use php_admin_value to set it.<br />

File Uploads<br />

File uploads combine the two dangers we’ve seen so far: user-modifiable data and<br />

the filesystem.While <strong>PHP</strong> 4 itself is secure in how it handles uploaded files, there are<br />

several potential traps for unwary programmers.<br />

Distrust Browser-Supplied Filenames<br />

Be careful using the filename sent by the browser.If possible, do not use this as the<br />

name of the file on your filesystem.It’s easy to make the browser send a file identified<br />

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

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

File Uploads | 289

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

Saved successfully!

Ooh no, something went wrong!