使用tp5写登录验证及修改密码

使用到的工具

1,thinkphp5.0自带的验证码: think-captcha

 可查看:  https://www.kancloud.cn/manual/thinkphp5/154295

2,前台页面框架: layui

3,引用到的文件: jquery.min.js   layui.css

 

登录样式最终图形

使用tp5写登录验证及修改密码_第1张图片

 

登录的html







   

登录控制器代码

 public function login()
    {
        if (Request::instance()->isPost()) { //判断是否是是否为 POST 请求
            $data = input('post.');
//            {admin_name: "admin", password: "123456", captcha: "m4r"}
            $username = $data['admin_name'];
            $password = md5($data['password']);
            $captcha = $data['captcha'];
            //调用验证器
            $validate = Loader::validate('Login');
            $data = ['username' => $username, 'password' => $password, 'captcha' => $captcha];
            //验证是否符合验证器里定义(验证码)的规范,不符合返回错误信息
            if (!$validate->check($data)) {
                return json($validate->getError());
            }
            //查询数据试库
            $where['admin_name'] = $username;
            $where['status'] = 1;
            $userInfo = Db::name('admin')->where($where)->find();
            if ($userInfo && $userInfo['password'] === $password) {
//                登录成功写入session
                Session::set('admin_id', $userInfo['id']);
                Session::set('admin_name', $userInfo['admin_name']);
                return json('jr');
            } else {
                return json('用户名或密码错误!');
            }
        } else {
            return $this->fetch('login');
        }
    }

 

 

验证器

class LoginValidate extends validate
{
    // 验证规则
    protected $rule = [
        'username|用户名' => 'require',
        'password' => 'require',
        'captcha' => 'require|checkCaptcha:null',
    ];
    protected $message = [
        'captcha.require' => '验证码错误!',  //验证码空的时候验证的它
    ];

    protected function checkCaptcha($value)  //验证码输入的进验证的它
    {
        $captcha = new Captcha();
        if ($captcha->check($value)) {
            return true;
        } else {
            return '验证码错误!';
        }
    }
}

 

 

修改密码

html

 

后台代码

class PasswordController extends BaseController
{
    public function passwordedit()
    {
        $admin_id = Session::get('admin_id');
        $data = input('post.');
        $admin = new AdminModel();
        $admin_info = $admin->where('id', $admin_id)->find();
        if ($data) {
            if ((md5($data['password_origin'])) == $admin_info['password']) {
                if ($data['password'] == $data['password_again']) {
                    $md5_password = md5($data['password']);
                    $result = AdminModel::where('id', $admin_id)->update(['password' => $md5_password]);
                    if ($result) {
                        return $this->success('修改成功啦!');
                    } else {
                        return $this->error('修改失败');
                    }
                } else {
                    return $this->error('再次输入不一致!');
                }
            } else {
                return $this->error('原密码输入错误!');
            }
        }
        return view();
    }
}

 

 

session失效后从新加载页面

 exit('');

 

你可能感兴趣的:(TP5,PHP)