Jump to content

Thoughts on an Anti-SQL Injection function?


dave_sticky

Recommended Posts

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.