Yii框架防止sql注入,xss攻击与csrf攻击的方法
框架(架构)  /  管理员 发布于 7年前   187
本文实例讲述了Yii框架防止sql注入,xss攻击与csrf攻击的方法。分享给大家供大家参考,具体如下: PHP中常用到的方法有: 调用: (必须放在接收数据之外) 注意: 表单提交值,为防止csrf攻击,控制器中需要加上: 更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家基于Yii框架的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( "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU", "/(<[^>]*)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 .....
//关闭csrfpiblic $enableCsrfValidation = false;
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号