TP5框架安全机制实例分析

这篇文章主要介绍了TP5框架安全机制,结合实例形式分析了thinkPHP5防止SQL注入以及表单合法性检测的安全性操作技巧,需要的朋友可以参考下

本文实例讲述了TP5框架安全机制。分享给大家供大家参考,具体如下:

防止sql注入

1、查询条件尽量使用数组方式,具体如下:

 $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方式(绑定参数),因为这里未使用PDO,所以不罗列,感兴趣的可自行查找相关资料。

表单合法性检测

1、配置insertFields和updateFields属性

 class UserInfoModelextends Model { // 数据表名字 protected $tureTableName ='user'; // 配置插入和修改的字段匹配设置(针对表单) protected $insertFields =array('name','sex','age'); protected $updateFields =array('nickname','mobile'); } 

上面的定义之后,当我们使用了create方法创建数据对象后,再使用add方法插入数据时,只会插入上面配置的几个字段的值(更新类同),具体如下:

 // 用户注册(示意性接口:插入) public function register() { // ... // 使用Model的create函数更安全 $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常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

以上就是TP5框架安全机制实例分析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » PHP编程