本文实例讲述了Yii框架防止sql注入,xss攻击与csrf攻击的方法。分享给大家供大家参考,具体如下:
PHP中常用到的方法有:
/* 防sql注入,xss攻击 (1)*/ function actionClean($str) { $str=trim($str); $str=strip_tags($str); $str=stripslashes($str); $str=addslashes($str); $str=rawurldecode($str); $str=quotemeta($str); $str=htmlspecialchars($str); //去除特殊字符 $str=preg_replace("/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\", "" , $str); $str=preg_replace("/\s/", "", $str);//去除空格、换行符、制表符 return $str; } //防止sql注入。xss攻击(1) public function actionFilterArr($arr) { if(is_array($arr)){ foreach($arr as $k => $v){ $arr[$k] = $this->actionFilterWords($v); } }else{ $arr = $this->actionFilterWords($arr); } return $arr; } //防止xss攻击 public function actionFilterWords($str) { $farr = array( "/<(\\/", "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU", "/select|insert|update|delete|drop|\'|\/\*|\*|\+|\-|\"|\.\.\/|\.\/|union|into|load_file|outfile|dump/is" ); $str = preg_replace($farr,'',$str); return $str; } //防止sql注入,xss攻击(2) public function post_check($post) { if(!get_magic_quotes_gpc()) { foreach($post as $key=>$val){ $post[$key] = addslashes($val); } } foreach($post as $key=>$val){ //把"_"过滤掉 $post[$key] = str_replace("_", "\_", $val); //把"%"过滤掉 $post[$key] = str_replace("%", "\%", $val); //sql注入 $post[$key] = nl2br($val); //转换html $post[$key] = htmlspecialchars($val); //xss攻击 } return $post; }
调用:
//防止sql $post=$this->post_check($_POST); //var_dump($post);die; $u_name=trim($post['u_name']); $pwd=trim($post['pwd']); if(empty($u_name)||empty($pwd)) { exit('字段不能非空'); } $u_name=$this->actionFilterArr($u_name); $pwd=$this->actionFilterArr($pwd); //防止sql注入,xss攻击 $u_name=$this->actionClean(Yii::$app->request->post('u_name')); $pwd=$this->actionClean(Yii::$app->request->post('pwd')); $email=$this->actionClean(Yii::$app->request->post('email')); //防止csrf攻击 $session=Yii::$app->session; $csrf_token=md5(uniqid(rand(),TRUE)); $session->set('token',$csrf_token); $session->set('token',time()); //接收数据 if($_POST) { if(empty($session->get('token')) && $session->get('token')!=Yii::$app->request->post('token') && (time()-$session->get('token_time'))>30){ exit('csrf攻击'); } //防止sql .....
(必须放在接收数据之外)
注意:
表单提交值,为防止csrf攻击,控制器中需要加上:
//关闭csrf piblic $enableCsrfValidation = false;
更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】
- 何耀珊.2006-收获【华纳】【WAV+CUE】
- 梁汉文1994-壹精选[华星][WAV+CUE]
- 洪荣宏1981-歹路不可行·相思雨(飞跃复刻版)[光美][WAV+CUE]
- 旅行团2015-10Day’s[来福胶泥][WAV+CUE]
- 龙千玉.2011-卡将哟【豪记】【WAV+CUE】
- 蔣志光.1993-多情多寂寞(国专)【BMG】【WAV+CUE】
- 陈慧娴.1996-雪映美白96演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 王子异《1996 Part.2》[FLAC/分轨][50.66MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[320K/MP3][155.91MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[FLAC/分轨][251.78MB]
- 黄韵玲2001-巴结音乐[全员集合][WAV+CUE]
- 郑绪岚2015-流金岁月·精选[首版][WAV+CUE]