Today’s entry is a guest post the from world-renowned Michelangelo van Dam, PHP master, community hero, consultant, and all around nice guy. Thanks for the post, Mike!
Yes, I’m repeating myself and apparently I need to. A quick search on github today revealed that 86,000+ people still use $_GET
in their mysql statements!
A few examples to get your attention:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
People please, don’t do it this way! You’re opening up your application to a whole lot of hurt and damage beyond repair if you implement this kind of code in production!
Remember this line: Filter input, escape output.
Filter all incoming data, no matter if they come from user input, database, web services or even from OCR using cameras!
Ensure this incoming data is always in the format you want and validates to rules you have defined. Yes, it’s a lot of work to filter and validate everything, but a lot less to clean up your database when they’ve done a “Little Bobby Tables” on your application.
Use an abstraction layer like PDO and properly prepare statements with named variables to ensure data will be properly escaped before it enters your database. Even if you haven’t taken the time to sanitise your data, this will be your final line of defence. Use it! Period!!!
This will be your basics to get started with PDO, but check out php.net/pdo for more information.
Getting a listing from MySQL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Getting a single item from MySQL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Now get moving to php.net/pdo and start learning doing things the right way!
— Michelangelo van Dam
Further Reading
If you’d like to dig into the topic of PHP security even further (and you do), check out these books that Mike recommends:
- Essential PHP Security by Chris Shiflett
- Pro PHP Security
- php|architect’s Guide to PHP Security by Ilia Alshanetsky