详细介绍php输入值/表单提交参数过滤有效防止sql注入的方法实例

PHP中文网
Release: 2023-02-28 09:58:01
Original
1076 people have browsed it

输入值/表单提交参数过滤可以有效防止sql注入或非法攻击,下面为大家介绍些不错的方法,感兴趣的不要错过

输入值/表单提交参数过滤,防止sql注入或非法攻击的方法:

代码如下:

/** 
* 过滤sql与
php
文件操作的关键字 
* @param 
string
 $string 
* @
return
 string 
* @author zyb <zyb_icanplay@163.com> 
*/ 
private function filter_
key
w
ord
( $string ) { 
$keyword = &#39;select|insert|up
date
|
delete
|\&#39;|\/\*|\*|\.\.\/|\.\/|
union
|into|load_
file
|outfile&#39;; 
$arr = 
explode
( &#39;|&#39;, $keyword ); 
$result = 
str_ireplace
( $arr, &#39;&#39;, $string ); 
return $result; 
} 
/** 
* 检查输入的数字是否合法,合法返回对应id,否则返回false 
* @param 
integer
 $id 
* @return mixed 
* @author zyb <zyb_icanplay@163.com> 
*/ 
protected function check_id( $id ) { 
$result = false; 
if
 ( $id !== &#39;&#39; && !is_
null
( $id ) ) { 
$var = $this->filter_keyword( $id ); // 过滤sql与php文件操作的关键字 
if ( $var !== &#39;&#39; && !is_null( $var ) && is_numeric( $var ) ) { 
$result = intval( $var ); 
} 
} 
return $result; 
} 
/** 
* 检查输入的字符是否合法,合法返回对应id,否则返回false 
* @param string $string 
* @return mixed 
* @author zyb <zyb_icanplay@163.com> 
*/ 
protected function check_str( $string ) { 
$result = false; 
$var = $this->filter_keyword( $string ); // 过滤sql与php文件操作的关键字 
if ( !empty( $var ) ) { 
if ( !
get_magic_quotes_gpc
() ) { // 判断magic_quotes_gpc是否为打开 
$var = 
addslashes
( $string ); // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤 
} 
//$var = 
str_replace
( "_", "\_", $var ); // 把 &#39;_&#39;过滤掉 
$var = str_replace( "%", "\%", $var ); // 把 &#39;%&#39;过滤掉 
$var = 
nl2br
( $var ); // 回车转换 
$var = 
htmlspecialchars
( $var ); // html标记转换 
$result = $var; 
} 
return $result; 
}
Copy after login

以上就是详细介绍php输入值/表单提交参数过滤有效防止sql注入的方法实例的内容,更多相关内容请关注PHP中文网(www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!