本文实例讲述了tp5框架简单登录功能实现方法。分享给大家供大家参考,具体如下:

登录方法,验证

public function login()
{
    if(request()->isget()){
      return view('login');
    }elseif(request()->ispost()){
      $model = new infomodel(); 
      $name = input('name'); //获取表单提交的姓名
      $pwd = input('password');//获取表单提交的密码
      if($model->loginverify($name,$pwd)){
        $verify = input('code'); //获取验证码的值
        $cap = new captcha(); //实例化验证码类
        if($cap->check($verify)){
          $this->success('登录成功','admin/showindex');//登录成功跳转首页
          /*echo '登录成功';*/
        }else{
          $this->error('验证码错误','admin/admin/login');
        }
      }
    }
}

表单

<div class="form-group">
    <div class="field field-icon-right">
      <input type="password" class="input input-big" name="password" placeholder="登录密码" data-validate="required:请填写密码" />
      <span class="icon icon-key margin-small"></span>
    </div>
  </div>
  <div class="form-group">
    <div class="field">
      <input type="text" class="input input-big" name="code" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码" />
      <img src="{:captcha_src()}" alt="" width="150" height="32" class="passcode" style="height:43px;cursor:pointer;" οnclick="this.src=this.src+'?'"> 
    </div>
</div>

model类,要与表名同名

<?php
namespace app\admin\model;
use think\model;
class info extends model
{
#登录验证
  public function loginverify($name,$pwd)
  {
    //$re = $this->where(["username =>'$name',pwd=>'$pwd'"])->find();
    $re = $this->where("username='$name' and pwd='$pwd'")->find();
    if($re){
      return $re;
    }else{
      return null;
    }
  }
}