think php 7天免登录

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
<form action="/admin/login/loginSuccess" method="post" style="width: 300px;">
    <div class="form-group">
        <label for="name">账号</label>
        <input type="text" class="form-control" name="account">
    </div>
    <div class="form-group">
        <label for="name">密码</label>
        <input type="password" class="form-control" name="password">
    </div>
    <div>
        <input type="checkbox" value="1"  name="free">7天免登录

    </div>
    <input type="submit" value="立即登录" class="btn btn-success">
</form>
</body>
</html>

 

 

 

 

 

 

控制器
<?php

namespace app\admin\controller;

use app\admin\model\LoginModel;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Validate;

class Login extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function login()
    {

        if (Cookie::has('name')) {
            $this->success('登录成功','/admin/login/loginShow');
        }
        return view();

    }

    public function loginSuccess()
    {
        $params = input();
        //验证
        $rule = [
            'account' => 'require',
            'password' => 'require',
        ];
        $msg = [
            'account.require' => '账号不可以为空',
            'password.require' => '密码不可以为空',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($params);
        if (!$result) {
            $this->error($validate->getError());
        }
//验证用户信息
        $data = LoginModel::getLoginInfo($params);
//        var_dump($data);
        if ($params['account'] != $data['name']) {
            $this->error('账号输入错误', '/admin/login/login');
        }
        if ($params['password'] != $data['password']) {
            $this->error('密码输入错误', '/admin/login/login');
        }
        //7天免登录
        if (isset($params['free'])) {
            Cookie::set('name', $data['name'], 3600 * 24 * 7);
            $this->success('登录成功','/admin/login/loginShow');
        }
    }
    public function loginShow(){
      $this->success('登录成功','/admin/exam/create');
    }


}

 

 

 

 模型

 

<?php

namespace app\admin\model;

use think\Model;

class LoginModel extends Model
{
    //
    protected $table='login';
    public static function getLoginInfo($params){
       return self::where('name',$params['account'])->find();
    }

 

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
<form action="/admin/login/loginSuccess" method="post" style="width: 300px;">
<div class="form-group">
<label for="name">账号</label>
<input type="text" class="form-control" name="account">
</div>
<div class="form-group">
<label for="name">密码</label>
<input type="password" class="form-control" name="password">
</div>
<div>
<input type="checkbox" value="1" name="free">7天免登录

</div>
<input type="submit" value="立即登录" class="btn btn-success">
</form>
</body>
</html>
posted @ 2021-08-23 15:14  王越666  阅读(90)  评论(0编辑  收藏  举报