Home > Article > Backend Development > Day 5 ThinkPHP step-by-step quick website splicing (5)
June 11th, sunny day. "Plums leave sore teeth, bananas are divided into green and window screens. As the day goes by, I wake up with ruthless thoughts and watch children catching willow flowers."
<?php class LoginAction extends Action { function index() { // 配置页面显示内容 $this->assign ( 'title', '后台管理系统' ); $this->display (); } // 用户登录页面 function login() { header ( "Content-Type:text/html; charset=utf-8" ); $username=$_POST['username']; $password=md5($_POST['password']); $User = D ( "User" ); // 参数的User必须首字母大写,否则自动验证功能失效! if (! $User->create ()) { $this->error ( $User->getError () ); } else { // 查找输入的用户名是否存在 if ($User->where ( "username ='$username' AND password = '$password'" )->find ()) { session ( username, $username ); $url = U ( '/Index/index/username/' . $username ); redirect ( $url, 5, '跳转中...' ); } else { $this->error ( '用户名或密码错误' ); } } }Among them, $User = D('User');
<span> means </span><span>Instantiate the User model, Will import the lib/model/usermodel.class.php file below the current item, and then instantize the UserModel class. Therefore, the actual code may be equivalent to the following: </span><span></span><span></span><pre name="code"><span style="font-family:Comic Sans MS;font-size:14px;"> import('@.Model.UserModel');
$User = new UserModel();</span></pre> <span>, create () create data creation data After the object is selected, the submitted form data will be automatically collected. </span><span>Before writing the form to the data table, there is often some data detection (whether the submitted user name meets the requirements) and processing (such as the comparison of the verification code in the example). The create() method supports automatic verification and automatic completion of data.使用 , In general, when using the M method to instantiate the model, it is impossible to instantiate the custom model class, and it is not possible to use automatic verification and automatic completion function. Therefore, it is recommended to use the D method to instantiate the model class. </span>
<span> </span>
2,
Written
Customized User model class<?php
/*
* 自定义User模型类
* 功能:1.完成自动验证功能
*/
class UserModel extends Model{
//自动验证成员属性(二维数组,每个数组代表一个验证规则)
//array('验证字段','验证规则','错误提示','验证条件','附加规则','验证时间'),
//验证字段:表单中的字段名称,也可以是表单中的一些辅助字段,例如验证码,重复密码等
//验证规则:
//错误提示:出现错误,抛出一个什么样的提示告知用户
//验证条件:参考手册6.15 (共有0,1,2三种值)
//附加规则:比如使用正则表达式验证,callback函数验证等,默认使用正则验证
//验证时间:1.新增时验证 2.编辑时验证 3.全部清况下验证
protected $_validate=array(
array('username', 'require', '用户名必须非空'),
array('username', 'callback_checklen', '用户名过长或过短', 0, 'callback'),
array('password', 'require', '密码必须非空' ),
array('repassword', 'require', '请重复输入密码' ),
// array('password', 'repassword','两次输入的密码不一致,请重新输入', 0, 'confirm'),
array('verify','require','验证码必须填写!'),
array('verify','callback_checkCode','验证码错误!',0,'callback'), //使用回调函数checkCode
);
//字段长度验证回调函数(ThinkPHP会自动帮我们传递参数)
function callback_checklen($data){
if(strlen($data)>15 || strlen($data)<5){
return false;
}
return true;
}
//验证码回调函数(ThinkPHP会自动帮我们传递参数)
function callback_checkCode($data){
if(md5($data)!=$_SESSION['verify']){
return false;
}
return true;
}
//自动完成,在create时自动执行
//array('填充字段','填充内容','填充条件','附加规则');
//填充字段
protected $_auto=array(
array('password','md5',3,'function'),
array('ip','callback_returnip',1,'callback'),
array('createtime','time',1,'function'),
);
function callback_returnip(){
return $_SERVER['REMOTE_ADDR'];
}
}
?>
The definition format is: array(
array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]),
...... );
Description Auxiliary fields, such as confirm password and verification code, etc. When there are individual validation rules that have nothing to do with fields, the validation fields can be set at will. For example, expire validity rules have nothing to do with form fields. If field mapping is defined, the validation field name here should be the actual data table field rather than the form field.
CREATE TABLE `think_news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`subject` varbinary(256) NOT NULL,
`createtime` int(11) NOT NULL,
`lastmodifytime` int(11) NOT NULL,
`message` mediumtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
<li><a class="shortcut-button" href="__URL__/news"><span> <img src="__PUBLIC__/Images/admin/icons/pencil_48.png" alt="icon" /><br />
编辑新闻 </span></a></li>
function news(){
//跳转到News控制器的index方法
redirect(U('/News/index'),0, '编写新闻');
}
The above introduces the fifth day of ThinkPHP step-by-step quick website splicing (5), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.