前端之家收集整理的这篇文章主要介绍了
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@本文实例讲述了PHP实现表单提交数据的验证处理功能。分享给大家供大家参考,具体如下:
@H_
404_0@<span style="font-size: medium">
防XSS攻击代码:
<div class="jb51code">
<pre class="brush:
PHP;">
/**
- 安全过滤函数
-
- @param $string
- @return string
/
function safe_replace($string) {
$string = str_replace('%20','',$string);
$string = str_replace('%27',$string);
$string = str_replace('%2527',$string);
$string = str_replace('',$string);
$string = str_replace('"','"',$string);
$string = str_replace("'",$string);
$string = str_replace(';',$string);
$string = str_replace('<','<',$string);
$string = str_replace('>','>',$string);
$string = str_replace("{",$string);
$string = str_replace('}',$string);
$string = str_replace('\',$string);
return $string;
}
PHP;">
PHP
$user_name = strim($_REQUEST['user_name']);
function strim($str)
{
//trim()
函数移除字符串两侧的空白字符或其他预定义字符。
//htmlspecialchars()
函数把预定义的字符转换为 HTML 实体(防xss攻击)。
//预定义的字符是:
//& (和号)成为 &
//" (双引号)成为 "
//' (单引号)成为 '
//< (小于)成为 <
//> (大于)成为 >
return quotes(htmlspecialchars(trim($str)));
}
//防
sql注入
function quotes($content)
{
//if $content is an array
if (is_array($content))
{
foreach ($content as $key=>$value)
{
//$content[$key] =
MysqL_real_escape_string($value);
/*addslashes()
函数返回在预定义字符之前
添加反斜杠的字符串。
预定义字符是:
单引号(')
双引号(")
反斜杠(\)
NULL */
$content[$key] = addslashes($value);
}
} else
{
//if $content is not an array
//$content=
MysqL_real_escape_string($content);
$content=addslashes($content);
}
return $content;
}
?>
$v)
{
if(preg_match($pattern,$k,$match))
{
die("
sql Injection denied!");
}
if(is_array($v))
{
filter_injection($request[$k]);
}
else
{
if(preg_match($pattern,$v,$match))
{
die("
sql Injection denied!");
}
}
}
}