Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

How to Protect from SQL Injection in PhP based website

3.80/5 (6 votes)
20 Jun 2012CPOL2 min read 40.8K  
How to Protect from SQL Injection in PhP based website

How to Protect from SQL Injection in PHP Based Website

One of the common methods that are being used by hackers is SQL INJECTION.

Sites get hacked by the sql injection due to the loop hole that is left by developers most of the times while developing a web application. I will be explaining you today how to avoid SQL INJECTION when you are developing a web application with PHP. I will be explaining with the help of an example, suppose we have text fields on our form

  1. User Name
  2. Password

and a login button.

When we login, the validation for the valid user is checked on the back-end. If the user is a valid user, he logs into the system else an error message "incorrect username or password" is shown.

What happens on the back-end,

$userName=$_POST[‘userName’];
$password =$_POST[‘password’];
$sqlQuery="select * from users where user_name= ‘".$userName."’ and user_password= ‘".$password."’ ;  ";

This is where the developer has left a loop hole if instead of password I enter  ‘ or ‘a’=’a  the password field has the value

$password is  ‘or ‘a’=’a

Lets place this value in query and the query becomes

$sqlQuery="select * from users where user_name= ‘".$userName."’ and user_password=’ ‘or ‘a’=’a’;   ";

You can see clearly, password doesn’t match but the other statement a=a matches so OR operator will work and the user will login into the system without knowing the actual password. I can even give you the names of some famous websites where you can inject sql or use this technique.

How to Avoid It

Don’t treat the field values as mentioned above

Use this function

function BlockSQL Injection($str){

return str_replace(array("'",""","'",'"'), array("'",""","'","""), $str);
}

This will replace the characters( that can break the string) in the string.

So you can use this function as

$userName= BlockSQL Injection ($_POST[‘userName’]);
$password = BlockSQL Injection ($_POST[‘password’]);

Now the hacker wont be able to break the QUERY STRING.

We have many frameworks in PHP that provide this functionality such as quotes_to_entities($string) in CODE IGNITER.

Use some desgin pattern when you are building a big application, model, controller, your view layers and DAO (data access object layer) must be implemented to make it losely coupled and extensible.

A huge number of sites have been developed in core php, where we don’t use any framework. Wordpress is very secure but when it comes to PLUGINS (that we donwload and use), they can have the loop holes inside them. Stay alert while developing web applications, you never know when you are gonna get hacked. Stay blessed!

Good Luck !

chets...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)