搜索型SQL注入解决方法

1.一般搜索型为get方式,但是如果不过滤一些特殊字段就会有漏洞,如:

$sql="select * from 表名 where name like '%".$key."' ;

像这种在输入框输入特殊字符如:or,and,update等,会使等式成立,搜索出全部内容,这个是错误的

2.解决方法:

function match_chinese($chars,$encoding='utf8'){ //只保留中文、英文和数字 
    $pattern =($encoding=='utf8')?'/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u':'/[\x80-\xFF]/'; 
    preg_match_all($pattern,$chars,$result); 
    return join('',$result[0]); 
}

3.获取关键词:

$kw = match_chinese(addslashes(htmlspecialchars($_GET['kw'])));//通过地址栏获取搜索词

路是自己走出来的,而不是选出来的。
原文地址:https://www.cnblogs.com/mo3408/p/15741727.html