表单提交的字段合法性检测,可以更好的保护数据的安全性。这一特性是3.1安全特性中的一个重要部分。这篇文章主要介绍了ThinkPHP3.1的字段合法性检测,需要的朋友
ThinkPHP3.1版增加了表单提交的字段合法性检测,可以更好的保护数据的安全性。这一特性是3.1安全特性中的一个重要部分。
表单字段合法性检测需要使用create方法创建数据对象的时候才能生效,具体有两种方式:
一、属性定义
可以给模型配置insertFields 和 updateFields属性用于新增和编辑表单设置,使用create方法创建数据对象的时候,不在定义范围内的属性将直接丢弃,避免表单提交非法数据。
insertFields 和 updateFields属性的设置采用字符串(逗号分割多个字段)或者数组的方式,例如:
class UserModel extends Model{ protected $insertFields = array('account','password','nickname','email'); protected $updateFields = array('nickname','email'); }
设置的字段应该是实际的数据表字段,而不受字段映射的影响。
在使用的时候,我们调用create方法的时候,会根据提交类型自动识别insertFields和updateFields属性:
D('User')->create();
使用create方法创建数据对象的时候,新增用户数据的时候,就会屏蔽'account','password','nickname','email' 之外的字段,编辑的时候就会屏蔽'nickname','email'之外的字段。
下面是采用字符串定义的方式,同样有效:
class UserModel extends Model{ protected $insertFields = 'account,password,nickname,email'; protected $updateFields = 'nickname,email'; }
二、方法调用
如果不想定义insertFields和updateFields属性,或者希望可以动态调用,可以在调用create方法之前直接调用field方法,例如,实现和上面的例子同样的作用:
在新增用户数据的时候,使用:
$User = M('User'); $User->field('account,password,nickname,email')->create(); $User->add();
而在更新用户数据的时候,使用:
$User = M('User'); $User->field('nickname,email')->create(); $User->where($map)->save();
这里的字段也是实际的数据表字段。field方法也可以使用数组方式。
使用字段合法性检测后,,你不再需要担心用户在提交表单的时候注入非法字段数据了。显然第二种方式更加灵活一些,根据需要选择吧!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.