Click here to Skip to main content
Click here to Skip to main content

How to Protect from SQL Injection in PhP based website

, 20 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionWordpress plugins PinmemberMember 1094055211-Jul-14 7:20 
GeneralMy vote of 5 Pinmembergndnet12-Jul-12 6:10 
Bug[My vote of 1] A very bad idea Pinmemberjakubmacek22-Jun-12 5:20 
GeneralMy vote of 1 Pinmemberimagiro20-Jun-12 22:43 
GeneralMy vote of 5 PinmemberFarhan Ghumra20-Jun-12 21:21 
GeneralRe: My vote of 5 PinmemberOnskee122-Jun-12 11:42 
Seriously? How long have you been friends with the author?
GeneralNot very inclusive PinmemberOnskee120-Jun-12 6:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 20 Jun 2012
Article Copyright 2012 by chetan dudhagara
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid