Jos*_*ren 1 php mysql symbols sql-injection character-encoding
我正在尝试编写一个php函数来阻止MySQL注入尝试.我正在做的是使用str_replace()删除符号并用它们的HTML字符代码替换它们.我的问题是代码都包含 但我也想用他们的代码替换这些符号.如何在不将代码更改为以下内容的情况下执行此操作:
&;&338;#35;32;
Run Code Online (Sandbox Code Playgroud)
这是我的功能:
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;
}
Run Code Online (Sandbox Code Playgroud)
And*_*are 11
你看过了mysql_real_escape_string吗?
转义未转义字符串中的特殊字符,同时考虑连接的当前字符集,以便将其放在mysql_query()中是安全的.