Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
Preventing MYSQL Injection Attacks!
Name: tubs Date: January 15, 2009 at 09:55:46 Pacific OS: Microsoft Windows Pista CPU/Ram: 2.394 GHz / 3070 MB Product: Hewlett-packard / Gj311aa-abu m8180.uk Subcategory: PHP
Comment:
Hey,
Im currently working on a small script for my website it is powered by a MYSQL DB and I have come in to one big problem…
I have created a function to stop those pesky MYSQL injection’s but it likes to remove all ‘ from the text that I input.
Only problem is that I can’t use it as punctuation how could I stop it form replacing ‘ with \’
Name: tubs Date: January 15, 2009 at 13:34:04 Pacific
Reply:
oh and i forgot to add that im useing mysql_real_escape_string and strip_tags
0
Response Number 2
Name: Fist (by fmwap) Date: January 16, 2009 at 20:19:51 Pacific
Reply:
use stripslashes after reading the data out of the database.
Technically though, you should be using a prepared statement & then you don't need to worry about quoting.
0
Response Number 3
Name: Michael J (by mjdamato) Date: January 17, 2009 at 01:54:13 Pacific
Reply:
I'm guessing your PHP configuraion has Magic Quotes turned on: http://www.php.net/magic_quotes When Magic Quotes is turned on any data coming from a form input will automatically have slashes added to the values. Then when mysql_real_escape_string() is used it adds additional slashes on top of that!
User Input: Bob's Store POST Data: Bob\'s Store mysql_real_escape_string() output: Bob\\\'s Store
As Fist states you would want to run strip_slashes on the POST data and then run mysql_real_escape_string() on it. However, if you want your code to be portable - move to any server - you need to code such that it will work whether magic quotes are on or off.
Instead of using mysql_real_escape_string() directly, you can create a custom function that will work no matter the environment.
function dbSafe($string)
{
if (get_magic_quotes_gpc())
{
$string = stripslashes($string);
}
return mysql_real_escape_string($string);
}
Summary: why is there a notice? the most dangerous reason is probably because by not defining all variables, people can do injections... let's see, if you have a script like this: <?php /* code to determine...
Summary: When I was looking to secure sites against SQL Injection, I found this site useful in explaining how to defend against them. Maybe it will help you too: http://www.sitepoint.com/article/sql-injection-...
Summary: Hey Michael, I fixed this so it works to where I need it. With the time you gave me it helped me learn Ajax, again thanks. I used some of your input crossed it with mine and came up with the solution....