This article mainly introduces the solution to the failure of ThinkPHP automatic verification. It has certain reference value. Now I share it with you. Friends in need can refer to it.
Quote ThinkPHP2.0 Development Manual: ThinkPHP Manual type checking is only for database-level verification, so the system also has a built-in automatic verification function of data objects to complete the business rule verification of the model. In most cases, the data objects are created by the $_POST data submitted by the form.
Copy code The code is as follows:
/* * 登陆 */ public function Login(){ if($_POST['submit']){ $DB = D('Login');//自定义Model处理 //if里面就是ThinkPHP的自动验证了. if(!$DB->create()){ $this->redirect('Index/Login', '', 3, '错误信息: '.$DB->getError().'<br/>系统将于3秒后返回重新登陆...'); }else{ $con['LoginName'] = $_POST['username']; $con['LoginPwd'] = md5($_POST['userpwd']); $list = $DB->where($con)->find(); if(count($list)>0){ echo 'ok'; }else{ $this->redirect('Index/Login', '', 3, '错误信息: 用户名或密码错误<br/>系统将于3秒后返回重新登陆...'); } } return ; } //这里只是将模板文件的地址封装了一下. A('Public')->ShowPage('login'); }
Copy code The code is as follows:
<?php class LoginModel extends Model { // 设置数据表 protected $tableName = 'admin'; // 自动验证设置 protected $_validate = array( array('username','require','用户名必须!', 1), array('userpwd','require','密码必须!', 1), ); /* 自动填充 如果不能自动验证,将这段代码取消注释看看. protected $_auto = array( array('status','1',self::MODEL_INSERT), array('create_time','time',self::MODEL_INSERT,'function'), );*/ /*引用ThinkPHP2.0开发手册:ThinkPHP手册类型检查只是针对数据库级别的验证,所以系统还内置了数据对象的自动验证功能来完成模型的业务规则验证,而大多数情况下面,数据对象是由表单提交的$_POST数据创建。需要使用系统的自动验证功能,只需要在Model类里面定义$_validate属性 */ /*它这里说了,只需要在Model类里面定义$_validate属性,但是在使用ThinkPHP2.1的时候,的的确确不能通过验证,$DB->getError()无错误原因返回,且刷新的时候$DB->getError()返回"令牌表单错误" */ } ?>
Related recommendations:
How to implement asynchronous transmission technology using ThinkAjax built in ThinkPHP
The above is the detailed content of Solution to ThinkPHP automatic verification failure. For more information, please follow other related articles on the PHP Chinese website!