Read + Write + Report
Home | Start a blog | About Orble | FAQ | Blogs | Writers | Paid | My Orble | Login

How to prevent MySQL injection attacks?

December 21st 2010 08:18
1. What is MySQ injection?
SQL injection refers to the act of someone inserting a MySQL statement to be run on your database without your knowledge. Injection usually occurs when you ask a user for input, like their name, and instead of a name they give you a MySQL statement that you will unknowingly run on your database.
2. Example of MySQL injection.
Suppose we have SQL code:
----------------------------- -----------------

SELECT fieldlist
FROM table
WHERE field = '$EMAIL';

----------------------------- ---------

By entering EMAIL as "anything' OR 'x'='x",
the resulting SQL is:

----------------------------- ------------------

SELECT fieldlist
FROM table
WHERE field = 'anything' OR 'x'='x';

----------------------------- -------------

'x'='x' clause is guaranteed to be true no matter what the first clause is.
This OR clause of 1 will always be true and so every single entry in the "customers" table would be selected by this statement!
3. How to prevent MySQL injection attacks?
Injection Prevention - mysql_real_escape_string()
What mysql_real_escape_string does is to take a string that is going to be used in a MySQL query and return the same string with all SQL Injection attempts safely escaped. Basically, it will replace those troublesome quotes(') a user might enter with a MySQL-safe substitute, an escaped quote \'.
prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.
Note: mysql_real_escape_string function
only works if you are already connected to a database.
Example #1 Simple mysql_real_escape_string() example
<?php
$link = mysql_connect('mysql_host',
'mysql_user', 'mysql_password') OR die(mysql_error());
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
mysql_real_escape_string($use r), mysql_real_escape_string($pas sword));
?>

Jelly Fish
Jelly Fish
Jelly Fish

12
Vote
Add To: del.icio.us Digg Furl Spurl.net StumbleUpon Yahoo


   
subscribe to this blog 


   

   

   

Add A Comment

To create a fully formatted comment please click here.


CLICK HERE TO LOGIN | CLICK HERE TO REGISTER

Name or Orble Tag
Home Page (optional)
Comments
Bold Italic Underline Strikethrough Separator Left Center Right Separator Quote Insert Link Insert Email
Notify me of replies
Your Email Address
(optional)
(required for reply notification)
Submit
More Posts
1 Posts
1 Posts
1 Posts
106 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:
0

Jiansen Lu's Blogs

1570 Vote(s)
0 Comment(s)
37 Post(s)
1076 Vote(s)
0 Comment(s)
20 Post(s)
5628 Vote(s)
1 Comment(s)
164 Post(s)
5740 Vote(s)
51 Comment(s)
95 Post(s)
Moderated by Jiansen Lu
Copyright © 2012 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]