Home > php教程 > php手册 > 输入值/表单提交参数过滤有效防止sql注入的方法

输入值/表单提交参数过滤有效防止sql注入的方法

WBOY
Release: 2016-06-13 10:18:48
Original
985 people have browsed it

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

复制代码 代码如下:


/**
* 过滤sql与php文件操作的关键字
* @param string $string
* @return string
* @author zyb
*/
private function filter_keyword( $string ) {
$keyword = 'select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile';
$arr = explode( '|', $keyword );
$result = str_ireplace( $arr, '', $string );
return $result;
}

/**
* 检查输入的数字是否合法,合法返回对应id,否则返回false
* @param integer $id
* @return mixed
* @author zyb
*/
protected function check_id( $id ) {
$result = false;
if ( $id !== '' && !is_null( $id ) ) {
$var = $this->filter_keyword( $id ); // 过滤sql与php文件操作的关键字
if ( $var !== '' && !is_null( $var ) && is_numeric( $var ) ) {
$result = intval( $var );
}
}
return $result;
}

/**
* 检查输入的字符是否合法,合法返回对应id,否则返回false
* @param string $string
* @return mixed
* @author zyb
*/
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 ); // 把 '_'过滤掉
$var = str_replace( "%", "\%", $var ); // 把 '%'过滤掉
$var = nl2br( $var ); // 回车转换
$var = htmlspecialchars( $var ); // html标记转换
$result = $var;
}
return $result;
}

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template