search
Homephp教程php手册ThinkPHP自动验证失败的解决方法

引用ThinkPHP2.0开发手册:ThinkPHP手册类型检查只是针对数据库级别的验证,所以系统还内置了数据对象的自动验证功能来完成模型的业务规则验证,而大多数情况下面,数据对象是由表单提交的$_POST数据创建。

代码如下:
/*
* 登陆
*/
public function Login(){
if($_POST['submit']){
$DB = D('Login');//自定义Model处理
//if里面就是ThinkPHP的自动验证了.
if(!$DB->create()){
$this->redirect('Index/Login', '', 3, '错误信息: '.$DB->getError().'
系统将于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, '错误信息: 用户名或密码错误
系统将于3秒后返回重新登陆...');
}
}
return ;
}
//这里只是将模板文件的地址封装了一下.
A('Public')->ShowPage('login');
}

代码如下:
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()返回"令牌表单错误"
*/
}
?>
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

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.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft