《PHP學(xué)習(xí):Yii框架防止sql注入,xss攻擊與csrf攻擊的方法》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Yii框架防止sql注入,xss攻擊與csrf攻擊的方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
PHP學(xué)習(xí)本文實(shí)例講述了Yii框架防止sql注入,xss攻擊與csrf攻擊的方法.分享給大家供大家參考,具體如下:
PHP學(xué)習(xí)PHP中常用到的方法有:
PHP學(xué)習(xí)
/* 防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);
//轉(zhuǎn)換html
$post[$key] = htmlspecialchars($val); //xss攻擊
}
return $post;
}
PHP學(xué)習(xí)調(diào)用:
PHP學(xué)習(xí)
//防止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());
//接收數(shù)據(jù)
if($_POST)
{
if(empty($session->get('token')) && $session->get('token')!=Yii::$app->request->post('token') && (time()-$session->get('token_time'))>30){
exit('csrf攻擊');
}
//防止sql
.....
PHP學(xué)習(xí)(必須放在接收數(shù)據(jù)之外)
PHP學(xué)習(xí)注意:
PHP學(xué)習(xí)表單提交值,為防止csrf攻擊,控制器中需要加上:
PHP學(xué)習(xí)
//關(guān)閉csrf
piblic $enableCsrfValidation = false;
PHP學(xué)習(xí)更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
PHP學(xué)習(xí)希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/3011.html