All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
asp.net中Session对象的概念以及属性
Email地址加密javascript版_JavaScript技术_编程技术
asp.net2.0文章标题后加new或new图片_[Asp.Net教程]
SQL数据操作基础(初级)1_mssql学习_编程技术
Login控件也可添加图文验证码_[Asp.Net教程]
实例学习PHP如何实现在线发邮件_php资料_编程技术
按实际要求格式化显示DataGrid中字段值的方法_[Asp.Net教程]
PHP+MySQL应用中使用XOR运算加密算法_php资料_编程技术
SQLServer数据库维护计划莫名其妙就失效的解决办_[SQL Server教程]
.Net+MySQL组合开发(二),数据访问篇_[Asp.Net教程]
Reading number is top 10 pictures
西游四格漫画(二)
西游四格漫画(一)
From China fortress sora aoi1
8090后结婚的各种XX事
More attractive than sora aoi1
如果我是导演...
张家界的玻璃桥
Beautiful Japanese beauty(漂亮的日本美女)
The terra-cotta warriors1
福利福利。。。。。。
Download software ranking
软件工程思想
Tram sex maniac 2 (H) rar bag14
Popkart Cracked versions Mobile phone games
C++编程教程第三版
The Bermuda triangle3
Unix video tutorial2
虚拟机汉化软件
Unix video tutorial13
Eclipse 4.2.2 For Win32
DreamWeaver8
归海一刀 published in(发表于) 2014/2/17 7:40:35 Edit(编辑)
PHP防注入攻击过滤HTML代码函数_[PHP教程]

PHP防注入攻击过滤HTML代码函数_[PHP教程]

PHP防注入攻击过滤HTML代码函数_[PHP教程]
/***** 说明:
* 当附合要求的参数传递进filter函数后,filter()函数首先
* 把要字串中所有要过滤的标签$tag通过preg_match_all()
* 取出来,然后循环preg_match_all的匹配数组,通过preg_split()
* 函数分割每个标签为 "左边属性" = "右边值"的形式,再从要保
* 留的属性数组中循环,将preg_split()匹配的内容对应取出,构成
* 可以替换的值,后最通过str_replcae()替换掉字串中相应的标签
* 函数列表:
* function filter(&$str,$tag,$keep_attribute)
* function match($reg,&$str,$arr)
* function show($str,$title=’’,$debug = True)
* 使用示例:
* //取得搜狐新闻首页
* $str = @file_get_content("http://news。sohu.com");
* //过滤
* filter($str,’a’,’href,target,alt’);
* filter($str,’p’,’align’);
* show($str,’过滤后的内容’);
*************************************************/

$start_time = array_sum(explode(" ",microtime()));

$str = <<< HTML
site a
site b
site c
site d
site e

adasdfasdf


asdfasdfasdfasdf


asdfasdfasdf



asdfadsfasdf
asdfasdfadf
asdfasdf
HTML;

//显示原字串
show($str,’Html’);

/***/
//过滤
filter($str,’a’,’href,target,alt’);
filter($str,’p’,’align’);
filter($str,’font’,’color,alt’);

//显示过滤后的内容
show($str,’Result’);

//脚本运行时间
$run_time = array_sum(explode(" ",microtime())) - $start_time;
echo(’
Script Run Time: ’.$run_time.’
’);

/**
* 说明:过滤HTML字串
* 参数:
* $str : 要过滤的HTML字串
* $tag : 过滤的标签类型
* $keep_attribute :
* 要保留的属性,此参数形式可为
* href
* href,target,alt
* array(’href’,’target’,’alt’)
*/
function filter(&$str,$tag,$keep_attribute) {

//检查要保留的属性的参数传递方式
if(!is_array($keep_attribute)) {
//没有传递数组进来时判断参数是否包含,号
if(strpos($keep_attribute,’,’)) {
//包含,号时,切分参数串为数组
$keep_attribute = explode(’,’,$keep_attribute);
}else {
//纯字串,构造数组
$keep_attribute = array($keep_attribute);
}
}
echo("·过滤[$tag]标签,保留属性:".implode(’,’,$keep_attribute).’
’);

//取得所有要处理的标记
$pattern = "/<$tag(.*)<\/$tag>/i";
preg_match_all($pattern,$str,$out);

//循环处理每个标记
foreach($out[1] as $key => $val) {
//取得a标记中有几个=
$cnt = preg_split(’/ *=/i’,$val);
$cnt = count($cnt) -1;

//构造匹配正则
$pattern = ’’;
for($i=1; $i<=$cnt; $i++) {

$pattern .= ’( .*=.*)’;
}
//完成正则表达式形成,如/(.*<\/a>/i的样式
$pattern = "/(<$tag)$pattern(>.*<\/$tag>)/i";

//取得保留属性
$replacement = match($pattern,$out[0][$key],$keep_attribute);

//替换
$str = str_replace($out[0][$key],$replacement,$str);
}
}
/**
* 说明:构造标签,保留要保留的属性
* 参数:$reg : pattern,preg_match的表达式
* $str : string,html字串
* $arr : array,要保留的属性
* 返回:
* 返回经保留处理后的标签,如
* e。com
*/
function match($reg,&$str,$arr) {

//match
preg_match($reg,$str,$out);

//取出保留的属性
$keep_attribute = ’’;
foreach($arr as $k1=>$v1) {
//定义的要保留的属性的数组
foreach($out as $k2=>$v2) {
//匹配=后的数组
$attribute = trim(substr($v2,0,strpos($v2,’=’)));
//=前面的
if($v1 == $attribute) {
//要保留的属性和匹配的值的=前的部分相同
$keep_attribute .= $v2;
//保存此匹配部分的值
}
}
}

//构造返回值,结构如:aadd
$keep_attribute = $out[1].$keep_attribute.($out[count($out)-1]);
//返回值
Return $keep_attribute;
}
/**
* 显示字串内容
*/
function show($str,$title=’’,$debug = True) {

if($debug) {
if(is_array($str)) {
$str = print_r($str,True);
}
$txtRows = count(explode("\n",$str))+1;
echo($title.’:

’);
}
}
?>


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.