Home >Backend Development >PHP Tutorial >Solution to ThinkPHP automatic verification failure

Solution to ThinkPHP automatic verification failure

不言
不言Original
2018-06-06 15:43:472096browse

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(&#39;Index/Login&#39;, &#39;&#39;, 3, &#39;错误信息: &#39;.$DB->getError().&#39;<br/>系统将于3秒后返回重新登陆...&#39;); 
}else{ 
$con[&#39;LoginName&#39;] = $_POST[&#39;username&#39;]; 
$con[&#39;LoginPwd&#39;] = md5($_POST[&#39;userpwd&#39;]); 
$list = $DB->where($con)->find(); 
if(count($list)>0){ 
echo &#39;ok&#39;; 
}else{ 
$this->redirect(&#39;Index/Login&#39;, &#39;&#39;, 3, &#39;错误信息: 用户名或密码错误<br/>系统将于3秒后返回重新登陆...&#39;); 
} 
} 
return ; 
} 
//这里只是将模板文件的地址封装了一下. 
A(&#39;Public&#39;)->ShowPage(&#39;login&#39;); 
}

Copy code The code is as follows:

<?php 
class LoginModel extends Model { 
// 设置数据表 
protected $tableName = &#39;admin&#39;; 
// 自动验证设置 
protected $_validate = array( 
array(&#39;username&#39;,&#39;require&#39;,&#39;用户名必须!&#39;, 1), 
array(&#39;userpwd&#39;,&#39;require&#39;,&#39;密码必须!&#39;, 1), 
); 
/* 自动填充 如果不能自动验证,将这段代码取消注释看看. 
protected $_auto = array( 
array(&#39;status&#39;,&#39;1&#39;,self::MODEL_INSERT), 
array(&#39;create_time&#39;,&#39;time&#39;,self::MODEL_INSERT,&#39;function&#39;), 
);*/ 
/*引用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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn