登录
控制器
<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Admin;
class Login extends Controller
{
public function login()
{
if(request()->isPost()){
$admin=new Admin();
$data=input('post.');
$num=$admin->login($data);
if($num==3){
$this->success('信息正确,正在为您跳转...','index/index');
}elseif($num==4){
$this->error('验证码错误');
}
else{
$this->error('用户名或者密码错误');
}
}
return $this->fetch();
}
}
模型
<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Admin extends Model
{
public function login($data){
$captcha = new \think\captcha\Captcha();
if (!$captcha->check($data['code'])) {
return 4;
}
$user=Db::name('admin')->where('username','=',$data['username'])->find();
if($user){
if($user['password'] == md5($data['password'])){
session('username',$user['username']);
session('uid',$user['id']);
return 3; //信息正确
}else{
return 2; //密码错误
}
}else{
return 1; //用户不存在
}
}
}
前台模板
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><!--Head--><head>
<meta charset="utf-8">
<title>skalpat</title>
<meta name="description" content="login page">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--Basic Styles-->
<link href="__PUBLIC__/style/bootstrap.css" rel="stylesheet">
<link href="__PUBLIC__/style/font-awesome.css" rel="stylesheet">
<!--Beyond styles-->
<link id="beyond-link" href="__PUBLIC__/style/beyond.css" rel="stylesheet">
<link href="__PUBLIC__/style/demo.css" rel="stylesheet">
<link href="__PUBLIC__/style/animate.css" rel="stylesheet">
</head>
<!--Head Ends-->
<!--Body-->
<body>
<div class="login-container animated fadeInDown">
<form action="" method="post">
<div class="loginbox bg-white">
<div class="loginbox-title">SIGN IN</div>
<div class="loginbox-textbox">
<input class="form-control" name="username" type="text">
</div>
<div class="loginbox-textbox">
<input class="form-control" name="password" type="password">
</div>
<div class="loginbox-textbox">
<input class="form-control" name="code" style="margin:10px 0;width:80px;float:left;" type="text">
<img style="float:left; cursor:pointer;" src="{:captcha_src()}" alt="captcha" onclick="this.src='{:captcha_src()}?'+Math.random();" />
</div>
<div class="loginbox-submit">
<input class="btn btn-primary btn-block" value="Login" type="submit">
</div>
</div>
<div class="logobox">
<p class="text-center">skalpat</p>
</div>
</form>
</div>
<!--Basic Scripts-->
<script src="__PUBLIC__/style/jquery.js"></script>
<script src="__PUBLIC__/style/bootstrap.js"></script>
<script src="__PUBLIC__/style/jquery_002.js"></script>
<!--Beyond Scripts-->
<script src="__PUBLIC__/style/beyond.js"></script>
</body><!--Body Ends--></html>
退出功能
控制器
public function logout(){
session(null);
$this->success('退出成功!','Login/login');
}