Possible Duplicate:
Best way to stop SQL Injection in PHP
Recently, I got my database wiped through a hacker inserting a DROP table command through a signup form.
This annoyed me and got me thinking:
How can I prevent mysql injection in php, before the information is sent to the database, e.g is there a way to detect that the user is trying to inject bad code into the database that can wipe it, and if so, detect it and display an error?
Also, is there a way to detect the mysql injection after it has been added, so when I am displaying the query, if it is the delete injection code, don’t display it.
Thanks again, I apologise for any waffle.
Use either PDO or Mysqli with the binding syntax. This alone will prevent most injection attacks.
Example:
In the above example, trying to escape the
:emailbinding to insert aDROP TABLEwon’t work.You still need to be careful with user-provided data. For instance, if the user provides a
$docIdfor aget documentquery, make sure they’re authorized for the document being requested. (And not just guessing a$docIdbelonging to some other user).