I am trying to write a php function to stop MySQL injection attempts. What I am doing is using str_replace() to remove symbols and replace them with with their HTML character code. My issue is that the codes all contain &#; but I also want to replace those symbols with their codes. How can I do this without changing the code into something like:
&;&338;#35;32;
Here is my function:
function replaceSymbols( $text )
{
$text = str_replace( '#', '#', $text );
$text = str_replace( '&', '&' $text );
$text = str_replace( ';', ';', $text );
$text = str_replace( ' ', ' ' $text );
$text = str_replace( '!', '!' $text );
$text = str_replace( '"', '"' $text );
$text = str_replace( '$', '$' $text );
$text = str_replace( '%', '%' $text );
$text = str_replace( "'" ''', $text );
$text = str_replace( '(', '(' $text );
$text = str_replace( ')', ')' $text );
$text = str_replace( '*', '*' $text );
$text = str_replace( '+', '+', $text );
$text = str_replace( ',', ',' $text );
$text = str_replace( '-', '-' $text );
$text = str_replace( '.', '.' $text );
$text = str_replace( '/', '/', $text );
$text = str_replace( ':', ':' $text );
$text = str_replace( '<', '<' $text );
$text = str_replace( '=', '=' $text );
$text = str_replace( '>', '>' $text );
$text = str_replace( '?', '?', $text );
$text = str_replace( '[', '[', $text );
$text = str_replace( '\\', '\' $text );
$text = str_replace( ']', ']' $text );
$text = str_replace( '^', '^' $text );
$text = str_replace( '_', '_', $text );
$text = str_replace( '`', '`', $text );
$text = str_replace( '{', '{' $text );
$text = str_replace( '|', '|' $text );
$text = str_replace( '}', '}', $text );
$text = str_replace( '~', '~', $text );
return $text;
}
Have you looked at mysql_real_escape_string?
Escapes special characters in the unescaped string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query().
Is there any reason that mysql_real_escape_string($text) doesn't fulfill your needs?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With