欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TP5框架安全機制實例分析

 更新時間:2020年04月05日 10:50:55   作者:Anwug!  
這篇文章主要介紹了TP5框架安全機制,結合實例形式分析了thinkPHP5防止SQL注入以及表單合法性檢測的安全性操作技巧,需要的朋友可以參考下

本文實例講述了TP5框架安全機制。分享給大家供大家參考,具體如下:

防止sql注入

1、查詢條件盡量使用數(shù)組方式,具體如下:

$wheres = array();
 
$wheres['account'] = $account;
 
$wheres['password'] = $password;
 
$User->where($wheres)->find();

2、如果必須使用字符串,建議使用預處理機制,具體如下:

$User = D('UserInfo');
 
$User->where('account="%s" andpassword="%s"',array($account,$password))->find();

3、可以使用PDO方式(綁定參數(shù)),因為這里未使用PDO,所以不羅列,感興趣的可自行查找相關資料。

表單合法性檢測

1、配置insertFields和updateFields屬性

class UserInfoModelextends Model {
 
   // 數(shù)據(jù)表名字
 
   protected $tureTableName ='user';
 
   // 配置插入和修改的字段匹配設置(針對表單)
 
   protected $insertFields =array('name','sex','age');
 
   protected $updateFields =array('nickname','mobile');
 
}

上面的定義之后,當我們使用了create方法創(chuàng)建數(shù)據(jù)對象后,再使用add方法插入數(shù)據(jù)時,只會插入上面配置的幾個字段的值(更新類同),具體如下:

// 用戶注冊(示意性接口:插入)
 
   public function register() {
 
     // ...
 
     // 使用Model的create函數(shù)更安全
 
     $User= D('UserInfo');
 
     $User->create();
 
     $ID= $User->add();
 
     if($ID) {
 
        $result= $User->where('id=%d',array($ID))->find();
 
        echo json_encode($result);
 
     }
 
     // ...
 
   }

2、使用field方法直接處理

// 插入
 
M('User')->field('name,sex,age')->create();
 
// 更新
 
M('User')->field('nickname,mobile')->create();

更多關于thinkPHP相關內容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。

相關文章

最新評論