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.

If you have .htaccess capabilities on your server and Apache is configured to let you<br />

override Options, you can make the change yourself.<br />

For the most secure session variables possible, create your own session store (e.g., in<br />

a database). Details for creating a session store are given in Chapter 7.<br />

Don’t Use Files<br />

Because all scripts running on a machine run as the same user, a file that one script<br />

creates can be read by another, regardless of which user wrote the script.All a script<br />

needs to know to read a file is the name of that file.<br />

There is no way to change this, so the best solution is to not use files.As with session<br />

stores, the most secure place to store data is in a database.<br />

A complex workaround is to run a separate Apache daemon for each user.If you add<br />

a reverse proxy such as Squid in front of the pool of Apache instances, you may be<br />

able to serve 100+ users on a single machine.Few sites do this, however, because the<br />

complexity and cost are much greater than those for the typical situation, where one<br />

Apache daemon can serve web pages for thousands of users.<br />

Safe Mode<br />

Many ISPs have scripts from several users running on one web server.Since all the<br />

users who share such a server run their <strong>PHP</strong> scripts as the same user, one script can<br />

read another’s data files.Safe mode is an attempt to address this and other problems<br />

caused by shared servers.If you’re not sharing your server with other users that you<br />

don’t trust, you don’t need to worry about safe mode at all.<br />

When enabled through the safe_mode directive in your php.ini file, or on a per-directory<br />

or per-virtual host basis in your httpd.conf file, the following restrictions are<br />

applied to <strong>PHP</strong> scripts:<br />

• <strong>PHP</strong> looks at the owner of the running script and pretends * to run as that user.<br />

• Any file operation (through functions such as fopen( ), copy( ), rename( ), move( ),<br />

unlink( ), chmod( ), chown( ), chgrp( ), mkdir( ), file( ), flock( ), rmdir( ), and<br />

dir( )) checks to see if the affected file or directory is owned by the same user as<br />

the <strong>PHP</strong> script.<br />

• If safe_mode_gid is enabled in your php.ini or httpd.conf file, only the group ID<br />

needs to match.<br />

• include and require are subject to the two previous restrictions, with the exception<br />

of includes and requires of files located in the designated safe_mode_<br />

include_dir in your php.ini or httpd.conf file.<br />

* <strong>PHP</strong> can’t switch the user ID via a setuid( ) call because that would require the web server to run as root<br />

and on most operating systems it would be impossible to switch back.<br />

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