Hacker, Researcher and Author.

Protect Your Website Against SQL Injection

Hacker-one: “ YES, I DID IT !!! “

Hacker-two: “What ? “

Hacker-one:” I HACKED ANOTHER SITE!!! “

Hacker-two: “Great!!! How did you do that? “

Hacker-one:” SQL INJECTION !!! :p “

Yes, 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,


$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.


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’]);

Note: This approach would only work where data type is string, however not where the iinjection type is an integer. It's recommended to use PHP's built in function called mysql_real_escape_string or "Prepared statements" which is the best solution to the date.

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 design 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 solely 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, 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 !

About The Author

Danyal Sandeelo is a Software Developer at "breezecom", He is the newest member of Team RHA, He blogs on http://blog.votemypic.com. 


  1. Hum!! Nice one Welcome to RHA family but some more explanation will be helpful for people like new web developers


  2. I usually don’t comment on blogs but this blog inspired me to make one compliment as I know it’s not enough for the meaningful context in your writing as one could understand it easily, will refer this blog to my friends to gather such informative insights.

  3. did u teach how to facebook a/c and other emails...
    plz teach.... sir

  4. I recently moved to wordpress from blogger so i don't know a lot about php coding.can you please explain this more.i mean where can i add those functions on my code?

  5. Thanks for sharing this information with us. Your material is up to date and quite informative, I would like to bookmark this page so I can come here to read this again, as you have done a wonderful job.

  6. This is a great inspiring blog. I am pretty much pleased with your good work.You put really very helpful information.

  7. Nice post having excellent contents.This is exactly what I’ve been looking for.Thank you very good
    Logo Designs

  8. There Is Obviously a lot to know about this. I suppose you made Some Great points in the Feature also.

    Logo Design Tips

  9. Thanks for the informative writing. Would mind updating some good tips about it. I still wait your next place. ;)

  10. This is definitely an amazing website for a beginner to get started.

  11. Nice Article Bro.But simple way is to use parameterized queries instead of passing into them directly

  12. Hum!! Nice one Welcome to RHA family but some more explanation will be helpful for people like new web developers.
    t shirts printing

  13. Very interesting article. I've always been interested in knowing more about this.

  14. This is very nice one and gives indepth information. thanks for this nice article.
    relatiegeschenk telefoon

  15. This continually is amazing to me how bloggers such as yourself can find the time and also the dedication to keep on creating fantastic blog posts. This is wonderful and one of my have to read on the web. I simply want to say thanks

  16. http://www.craftberrybush.com/

  17. maximum commnet, really great post


© 2016 All Rights Reserved by RHA Info Sec. Top

Contact Form


Email *

Message *

Powered by Blogger.