1.Model层下声明一个AdminModel类文件:自定义check方法
<?php namespace Admin\Model; use Think\Model; class AdminModel extends Model { /*model层定义的属性tableName,如果表名和控制器名称一致则可以省略不写,但下面调用表时 则不能使用$this->tableName,而需要显示声明M('admin')*/ protected $tableName='admin'; /*验证后台登录账号输入是否正确 @param $username:用户名 @param $pwd :密码 return :返回查询用户ID或者false */ function check($username,$pwd){ $result = M($this->tableName)->where("user_name='$username' AND pwd='$pwd'")->getField('user_id'); if($result){ return $result; }else{ return $false; } } } ?>
2.控制器调用自定义模型中的方法进行账号密码的验证:
public function index(){
if(IS_POST){
$username=I('post.username');
$pwd=I('post.pwd','','md5');
/*通过调用模型来获取查询结果,此处D方法为thinkphp内置,意为采取自定义的方式实例化模型 相当于 $User = new \Home\Model\AdminModel(); 当 \Home\Model\AdminModel 类不存在的时候,D函数会尝试实例化公共模块下面的 \Common\Model\AdminModel 类 */
$result = D('admin')->check($username,$pwd);
if($result){
cookie('isLogin',1,10);
$this->redirect('Index/index');
}else{
cookie('isLogin',0);
$this->error('用户名或者密码输入有误!','',1);
}
}else{
$this->display('login');
}
}
原文链接:https://blog.csdn.net/living_ren/article/details/78947176
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~