PHP and MySQL Basics IV -- SQL Injection and Prepared Statements
SQL Injection is a highly feared and often misunderstood problem. The basic phobia is that someone hijacks your SQL request and suddenly has full access to everything in your database. Well, it usually is not that easy and it is actually easy to avoid. Rule 1: Never Trust User Supplied The usual example is something like a query SELECT * FROM customer_data WHERE customer_id='$id' and the programmer was expecting an integer for the customer_id . But a dastardly use inserts some horrible SQL code to pirate the information so the query looks like SELECT * FROM customer_data WHERE customer_id=1 OR customer_id > 0 and suddenly all your customer data is out free in the universe waiting for who knows what. The code could have checked to see if the value of customer_id was truly an integer or returning an error if not. The is_int function was designed to do just this. if is_int($customer_id) { //Do all the stuff we want to do if we have a integer //submitted for ...