1.防止Sql注入
在获取$_GET,$_POST数据时,判断get_magic_quotes_gpc()是否开启,若开始,使用stripslashes()去掉转义字符,获取干净的数据。然后在操作数据库时,再用addslashes()转义。
//获取干净字符方法
function getStrNoSlashes($name,$default="",$toEncoding="")
{if(isset($_REQUEST["$name"])) $sReturn = $_REQUEST["$name"];
else $sReturn = "";
if (get_magic_quotes_gpc()) $sReturn = stripslashes($sReturn);if($sReturn === "") return $default;
if($toEncoding != "") $sReturn = iconv("UTF-8",$toEncoding,$sReturn);
return $sReturn;
}
本文介绍了一种有效的防御SQL注入的方法,通过使用getStrNoSlashes函数来获取并清理$_GET和$_POST数据中的非法字符。该函数首先检查get_magic_quotes_gpc是否启用,并据此决定是否需要使用stripslashes去除转义字符。接着,在数据被用于数据库操作之前,使用addslashes进行转义。
3万+

被折叠的 条评论
为什么被折叠?



