Yii架構防止sql注入,xss攻擊與csrf攻擊的方法_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了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(      "/<(\\/?)(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  .....

(必須放在接收資料之外)

注意:

表單提交值,為防止csrf攻擊,控制器中需要加上:

//關閉csrfpiblic $enableCsrfValidation = false;

更多關於Yii相關內容感興趣的讀者可查看本站專題:《Yii架構入門及常用技巧總結》、《php優秀開發架構總結》、《smarty模板入門基礎教程》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於Yii架構的PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.