dave_sticky Posted September 8, 2009 Share Posted September 8, 2009 It is with a little bit of apprehension :-\ that I'm doing this, because I'm sure I'm about to open myself up to all sorts of ridiculing for wasting my time on a pointless (or indeed insecure / over engineered / processor power wasting) function, but after much looking around the net at Anti-SQL Injection stuff, I thought I'd steal borrow some ideas and create my own function... You know... For funsies. So, the idea of this function is that you can pass an array to it (like $_POST or $_GET) and it'll escape all the nasty characters for you so that you can safely build it into a query. Called like this: $safeArray = magicSlashes($_POST); function magicSlashes($array) { // To escape special characters for use in an SQL statement. global $link; // Database Connector. foreach ($array as $key => $value) { // For value in the array... if(get_magic_quotes_gpc()) { // If magic quotes is switched on... if(is_array($value)) { // And If we're looking at a multidimensional array (checkboxes)... foreach($value as $element) { // For each value in the sub array... $element = stripslashes($element); // Strip out magic_quotes' handywork... $safeArray[$key][] = mysql_real_escape_string($element,$link); // Escape it properly and put it in the safe array } // END FOREACH($value as $element). } else { // If we're not looking at a multidimensional array... $value = stripslashes($value); // Strip out magic_quotes' handywork... $safeArray[$key] = mysql_real_escape_string($value,$link); // Escape it properly and put it in the safe array } // END IF(is_array($value)). } else { // If magic quotes IS NOT on... if(is_array($value)) { // And If we're looking at a multidimensional array (checkboxes)... foreach($value as $element) { // For each value in the sub array... $safeArray[$key][] = mysql_real_escape_string($element,$link); // Escape properly and stick in the safe array. } } else { // if we're not looking at a multidimensional array... $safeArray[$key] = mysql_real_escape_string($value,$link); // Escape properly and stick in the safe array. } // END IF(is_array($value)). } // END IF(magic_quotes). } // END FOREACH($array as $key => $value). // Return the safe array. return $safeArray; } // END magicSlashes($array). I've not actually tested this, but it is based on a previous (and fully functioning) one that I did using addslashes() rather than mysql_real_escape_string(). Feedback / thoughts / critique / ridicule appreciated! Thanks Guys. (and sorry if I've posted this in the wrong area) Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 8, 2009 Share Posted September 8, 2009 Just use prepared statements using either MySQLi or PDO. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.