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

TP5框架安全機(jī)制實(shí)例分析

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

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

防止sql注入

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

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

2、如果必須使用字符串,建議使用預(yù)處理機(jī)制,具體如下:

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

3、可以使用PDO方式(綁定參數(shù)),因?yàn)檫@里未使用PDO,所以不羅列,感興趣的可自行查找相關(guān)資料。

表單合法性檢測

1、配置insertFields和updateFields屬性

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

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

// 用戶注冊(示意性接口:插入)
 
   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();

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

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

相關(guān)文章

最新評論