Simple best-practice idea to prevent sql injections
#1
Posted 16 February 2013 - 11:34 PM
Basically, you create a few functions like:
function ireq($x){ return intval($_REQUEST[$x]); }
function req($x){ return mysql_real_escape_string(trim($_REQUEST[$x])); }
function unescaped($x){ return $_REQUEST[$x]; }
Next, NEVER use $_REQUEST
Now, to check your site for SQL injection holes, you can just search for $_REQUEST and "unescaped(". You can even use this method to slowly rewrite other peoples code, by replacing each $_REQUEST and making sure the proper characters are escaped.
Has the added benifit of being MUCH fast to type - $i = req('i') vs $i = mysql_real_escape_string($_REQUEST['i']);
#2
Posted 17 February 2013 - 12:04 AM
- Besides unescaped() you could just as easily search for code using $_REQUEST directly and fix that.
#3
Posted 17 February 2013 - 12:09 AM

My rarely updated, incredibly rambing, questionably informative blog || Don't go to w3schools || Using 'global' is a sign of doing it wrong
#4
Posted 17 February 2013 - 12:57 AM
- Besides unescaped() you could just as easily search for code using $_REQUEST directly and fix that.
The advantage to using unescaped is that it at least shows you looked and made a consious decision to use unescaped data. Also, if you implement others code, you can replace their $_REQUEST's as needed.
#5
Posted 17 February 2013 - 04:01 AM
You shouldn't be using the old mysql_* functions at all. They've been soft deprecated. Instead, use either MySQLi or PDO with prepared statements. That will protect you from injection attacks.
Agreed 100%
#6
Posted 20 February 2013 - 04:44 AM
#7
Posted 21 February 2013 - 12:04 PM
You shouldn't be using the old mysql_* functions at all. They've been soft deprecated. Instead, use either MySQLi or PDO with prepared statements. That will protect you from injection attacks.
I agree, with prepared statements, SQL injections are almost 100% eliminated.
$username = 'Longstreet';
$query = $db->prepare("INSERT INTO names(username) VALUES(:username)");
$query->bindParam(':username', '$username', PDO::PARAM_STR);
$query->execute();
When I first started using PDO, I kept forgetting to execute the query and I can't tell you how many hours I had spend searching why the heck wasn't my code working
Edited by Stefany93, 21 February 2013 - 12:05 PM.
"Never take counsel of your fears!" - Stonewall Jackson
My site - http://dyulgerova.info
#8
Posted 21 February 2013 - 12:05 PM
They depricate everything =/
Nope, they deprecate stuff that don't work anymore or are harmful if used...
"Never take counsel of your fears!" - Stonewall Jackson
My site - http://dyulgerova.info
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












