防止sql注入漏洞可以用以下三个函数:
1.addslashes()用于单字节字符串的处理,他是强行加/
$username=addslashes($username);
2.mysql_escape_string用于多字节字符串的处理,不考虑连接的当前字符集
$username=mysql_escape_string($username);
3.mysql_real_escape_string()用于多字节字符串的处理,会判断字符集,但是对PHP版本有要求
示例:
$clean = array();
$mysql = array();
$clean['last_name'] = "O'Reilly";
$mysql['last_name'] = mysql_real_escape_string($clean['last_name']);
$sql = "INSERT
INTO user (last_name)
VALUES ('{$mysql['last_name']}')";
?>