Security of PHP scripts

Security of PHP scripts: Part III

This column will round off our three-page series on security PHP scripting by discussing three important issues: the safety of file uploads, issues surrounding error reporting, and PHP safe mode.

File uploads

PHP allows you to handle file uploads from users easily. The following script implements a basic upload form.

if(!isset($file)) {
<form method=post enctype="multipart/form-data" action="<? echo $PHP_SELF; ?>"?>
Upload a file: <input type=file name=file /> <br />
<input type=submit value="Upload" />
} else {
if(is_uploaded_file($file)) {
if($file_size > 1000000) {
echo "The uploaded file is too large";
} else {
move_uploaded_file($userfile, "/usr/local/apache/uploads/");
echo "Your file was successfully uploaded";
} else {
echo "Invalid upload. Try again with a valid file upload";

The first part of the script tests to see if the file, $file, was uploaded. If not, it presents the user with the upload form. Notice that the <form> element has an attribute 'enctype' set to 'multipart/form-data'. This is necessary when allowing file uploads.

The example script tests for an important security threat: whether or not the file named in $file is an uploaded file. When PHP accepts a file upload, it saves the uploaded file in a temporary directory and provides the script with its filename in the variable set in the script (in our example, $file). A malicious user could manipulate this situation. Consider an upload script that contained the code:

if(isset($file)) {
/* ... */

What if the user did not actually upload a file but rather passed a filename as follows: The script would effectively run: move("../bin/httpd","/usr/local/apache/uploads").Given that most people install their scripts in /usr/local/apache/htdocs, this would mean that PHP would move the location of the Apache binary (httpd) to another directory. If the server was to be stopped and restarted, the restart would fail since the Apache binary would not exist where expected. This would cause a complete denial of service.

To safeguard against this, you can also access upload file variables, such as $file, through the global array $HTTP_POST_FILES["file"].

The script tests for another security problem: the size of the file, $file_size. If this test did not take place, a user could upload files of any size and intentionally exhaust the server's disk space.

Security of error reporting

When developing a script, PHP's error warnings can be very useful in diagnosing bugs. However, once you publicly deploy your script it serves no purpose, and could expose details of the internals of your script to users. As such, it is always best to redirect errors to a log file. This can easily be done by setting the `error_log' directive in the PHP configuration file, php.ini.

Safe mode

Safe mode supplements the operating system's permission system by further isolating PHP from the rest of the file system. To turn on safe mode, set the 'safe_mode' directive in php.ini to 'On'.

In safe mode, PHP can only access files owned by the Web server user - usually called 'nobody'. This means a malicious user would not be able to manipulate a script to read system configuration and other important files.

Further still, scripts can be prohibited from using risky functions - such as those involved in file system manipulation, execution of external programs or any other kind of function - by explicitly disabling such functions using the 'disable_functions' directive. A comma-separated list of function names will ensure that they can never be used. An error such as the following will be generated: "Warning: system() has been disabled for security reasons. in safe.php on line 401".

Finally, whenever you deploy PHP on a public system always be sure to keep up with new releases of PHP to ensure that you are protected against any security problems with older releases.

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles


PCW Evaluation Team

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?