php防sql注入漏洞可以用什么函数

php防sql注入漏洞可以用什么函数
最新回答
风铃仙子

2022-04-28 01:43:10

1.函数的构建
function inject_check($sql_str) {
return eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str); // 进行过滤
}
function verify_id($id=null) {
if (!$id) { exit('没有提交参数!'); } // 是否为氏瞎空判断
elseif (inject_check($id)) { exit('提交的参数非法!'); } // 注射判断
elseif (!is_numeric($id)) { exit('提交的参数非法!'); } // 数字判断
$id = intval($id); // 整型化
return $id;
}?
function str_check( $str ) {
if (!get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否打开
$str = addslashes($str); // 进行过滤
}
$str = str_replace("_", "\_", $str); // 把 '_'过滤掉
$str = str_replace("%", "\%", $str); // 把 '%'过滤掉
return $str;
}
function post_check($post) {
if (!get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否为打开
$post = addslashes($post); // 进槐哗行magic_quotes_gpc没有打开的情况对提交数据的过滤
}
$post = str_replace("_", "\_", $post); // 把 '_'过滤掉
$post = str_replace("歼明空%", "\%", $post); // 把 '%'过滤掉
$post = nl2br($post); // 回车转换
$post = htmlspecialchars($post); // html标记转换
return $post;
}
2.函数的使用实例
<?php
if (inject_check($_GET['id']))
{
exit('你提交的数据非法,请检查后重新提交!');
}
else
{
$id = $_GET['id'];
//处理数据………………
}
?>