防止sql注入漏洞可以用以下三個函數(shù):
1.addslashes()用于單字節(jié)字符串的處理,他是強行加/
$username=addslashes($username);
2.mysql_escape_string用于多字節(jié)字符串的處理,不考慮連接的當前字符集
$username=mysql_escape_string($username);
3.mysql_real_escape_string()用于多字節(jié)字符串的處理,會判斷字符集,但是對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']}')";
?>