ThinkPHP 表单自动验证运用,thinkphp表单验证
ThinkPHP 表单自动验证运用,thinkphp表单验证
使用TP 3.2框架
public function add_post(){ //验证规则 $rule=array( array('name','require','请输入姓名',1),//必须验证name ); $m=M('user'); //获取name,sex,contact数据到模型,并验证 if(!$m->field('name,sex,contact')->validate($rule)->create()) $this->error($m->getError()); $result=$m->add(); if(!$result) $this->error('添加失败'); $this->success('添加成功',U('dir')); }
验证规则也可以写到模型里,但我感觉有些麻烦,一是有时候不同页面验证的方式会不一样,二是看到这个add_post事件里的代码,就清楚要接收什么数据,如何验证数据能够在第一眼有个大致了解,所以总结出了此方式。
ThinkPHP新版内置了表单令牌验证功能,可以有效防止表单的远程提交等安全防护。
表单令牌验证相关的配置参数有:'TOKEN_ON'=>true, // 是否开启令牌验证'TOKEN_NAME'=>'__hash__', // 令牌验证的表单隐藏字段名称'TOKEN_TYPE'=>'md5', //令牌哈希验证规则默认为MD5如果开启表单令牌验证功能,系统会自动在带有表单的模板文件里面自动生成以TOKEN_NAME为名称的隐藏域,其值则是TOKEN_TYPE方式生成的哈希字符串,用于实现表单的自动令牌验证。自动生成的隐藏域位于表单Form结束标志之前,如果希望自己控制隐藏域的位置,可以手动在表单页面添加 标识,系统会在输出模板的时候自动替换。如果在开启表单令牌验证的情况下,个别表单不需要使用令牌验证功能,可以在表单页面添加{__NOTOKEN__},则系统会忽略当前表单的令牌验证。如果页面中存在多个表单,建议添加标识,并确保只有一个表单需要令牌验证。模型类在创建数据对象的同时会自动进行表单令牌验证操作,如果你没有使用create方法创建数据对象的话,则需要手动调用模型的autoCheckToken方法进行表单令牌验证。如果返回false,则表示表单令牌验证错误。例如:$User = M("User"); // 实例化User对象// 手动进行令牌验证if (!$User->autoCheckToken($_POST)){// 令牌验证错误
给你看我写的一个例子:
//表单验证
protected $_validate=array(
//array('验证字段','验证规则','错误提示',验证条件,附加规则,验证时间)
array('uname','require','用户名必须验证!',1,'regex',3),
//array('username','','用户名已经存在',1,'unique',1),
array('pwd','require','密码必须填写!'),
array('pwd','checkPwd','密码长度不少于6位',1,'callback'),
);
function checkPwd(){
$password=$_POST['pwd'];
if(strlen($password)>=6){
return true;
}else {
return false;
}
}
//表单映射
protected $_map=array(
'uname'=>'username',
'pwd'=>'password',
);
//自动完成功能
protected $_auto=array(
//array(填充字段,填充内容,填充条件,附加规则) 填充条件:1,插入 2,更新 3,所有的
array('reg_date','getDate',1,'callback'),
array('password','md5',3,'function'),
);
function getDate(){
return date('Y-m-d H:i:s');
}

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

ThinkPHP6 backend management system development: Implementing backend functions Introduction: With the continuous development of Internet technology and market demand, more and more enterprises and organizations need an efficient, safe, and flexible backend management system to manage business data and conduct operational management. This article will use the ThinkPHP6 framework to demonstrate through examples how to develop a simple but practical backend management system, including basic functions such as permission control, data addition, deletion, modification and query. Environment preparation Before starting, we need to install PHP, MySQL, Com
