避免SQL注入漏洞可以用以下三个函数:
1.addslashes()用于单字节字符串的处理,他是强行加/
$username=addslashes($username);
2.mysql_escape_string用于多字节字符串的处理,不斟酌连接确当前字符集
$username=mysql_escape_string($username);
3.mysql_real_escape_string()用于多字节字符串的处理,会判断字符集,但是对PHP版本有要求
示例:
<?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']}')";
?>
本文来源:https://www.yuntue.com/post/61123.html | 云服务器网,转载请注明出处!