13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

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

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

Securing Your Code<br />

367<br />

the security of our system, we have given malicious users a huge helping h<strong>and</strong> in their<br />

attempts to compromise our system.<br />

If you are going to do it, do it properly.The Internet is particularly unforgiving to<br />

those prone to carelessness or laziness.The hardest part of sticking to this mantra is convincing<br />

a boss or paycheck signer that this is worthwhile. A few minutes teaching them<br />

about the negative effects (including those against the bottom line) of security lapses<br />

should be enough to convince them that the extra effort will be worthwhile in a world<br />

where reputation is everything.<br />

Securing Your Code<br />

Moving on to the next aspect of our approach to security—inspecting each of the components<br />

individually <strong>and</strong> looking at how to improve their security—we begin in this<br />

section by investigating the things we can do to help keep our code safe. Although we<br />

cannot show you everything you might want to do to cover all possible security threats<br />

(entire tomes have been devoted to these subjects), we can at least give some general<br />

guidelines <strong>and</strong> point you in the right direction. For some specific technology areas in<br />

<strong>PHP</strong> that we will use in later chapters, we will point out security concerns for these as<br />

we see them.<br />

Filtering User Input<br />

One of the most important things we can do in our web applications to make them<br />

more secure is to filter all user input.<br />

Application authors must filter all input that comes from external sources.This does<br />

not mean that we should design a system with the assumption that all our users are<br />

crooks.We still want them to feel welcome <strong>and</strong> indeed encourage them to use our web<br />

application.We just want to be sure that we are prepared at any point for misuse of our<br />

system.<br />

If we do this filtering effectively, we can reduce the number of external threats substantially,<br />

<strong>and</strong> massively improve the robustness of our system. Even if we are pretty sure<br />

that we trust the users, we cannot be certain that they do not have some type of spyware<br />

program or other such thing that is modifying or sending new requests to our server.<br />

Given the importance of filtering the input we get from external customers, we<br />

should take a look at the ways in which we might do this.<br />

Double-Checking Expected Values<br />

At times we will present the user with a range of possible values from which to choose,<br />

for things such as shipping (ground, express, overnight), state or province, <strong>and</strong> so on.<br />

Now, imagine if we were to have the following simple form:

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

Saved successfully!

Ooh no, something went wrong!