使用TP5框架怎么实现一个登录功能

这期内容当中小编将会给大家带来有关使用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"  ο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;
    }
  }
}

上述就是小编为大家分享的使用TP5框架怎么实现一个登录功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。