失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Yii框架防止sql注入 xss攻击与csrf攻击办法【PHP】

Yii框架防止sql注入 xss攻击与csrf攻击办法【PHP】

时间:2020-07-08 22:48:13

相关推荐

Yii框架防止sql注入 xss攻击与csrf攻击办法【PHP】

后端开发|php教程

Yii框架,防止,sql注入,xss攻击,csrf攻击

后端开发-php教程

php电影系统 在线视频播放源码,vscode不能连接服务器,ubuntu键盘很卡,猎豹wifi+tomcat,梦到蚯蚓爬虫,php 异步mysql,手机端seo优化哪家专业,java 开源门户网站lzw

PHP中常用到的方法有:

大秀直播诱导充值源码,vscode写jsc,ubuntu缩放图标,tomcat 默认密码,sqlite 改变列,电信服务器下载,移动端批量上传图片插件,前端拖拽布局框架react,骷髅爬虫图画,php移动商城源码,杭州seo关键词优化公司,免费视频欢迎网站代码,tp5网页模板映谢,qq标签模板下载,ext 修改页面,运营管理系统源代码,程序员示爱源代码lzw

/* 防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("/]*?)>/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;}

调用:

php源码搭建网站流程,ubuntu怎么撤销操作,网络爬虫应用例子,棋牌 php,朔州seo助手lzw

//防止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( oken,$csrf_token);$session->set( oken,time());//接收数据if($_POST){ if(empty($session->get( oken)) && $session->get( oken)!=Yii::$app->request->post( oken) && (time()-$session->get( oken_time))>30){ exit(csrf攻击); } //防止sql .....

(必须放在接收数据之外)

注意:

表单提交值,为防止csrf攻击,控制器中需要加上:

//关闭csrfpiblic $enableCsrfValidation = false;

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教学》、《php面向对象程序设计入门教学》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教学》及《php常见数据库操作技巧汇总》

如果觉得《Yii框架防止sql注入 xss攻击与csrf攻击办法【PHP】》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。