thinkPHP实现的验证码登录功能

不言
不言 原创
2023-03-25 11:34:01 2330浏览

这篇文章主要介绍了thinkPHP实现的验证码登录功能,分析了thinkPHP验证码登陆验证功能的相关实现技巧,需要的朋友可以参考下

本文实例讲述了thinkPHP实现的验证码登录功能。分享给大家供大家参考,具体如下:

使用thinkphp自带的验证,实现登录页面的账号密码+验证码的验证

<?php
  namespace Admin\Controller;
  use Think\Controller;
  use Think\Verify;
  class LoginController extends Controller{
    public function login(){
      if($_POST){
        $obj = new Verify();
        if($obj->check(I('post.yanzhengma','','trim'))){
          // 注释部分为另外一种从数据库中验证密码的方法
          // $data['name'] = I('post.user_name');
          // $data['psd'] = I('post.password');
          // $row = M('user')->where($data)->find();
          $name = I('post.user_name');
          $psd = I('post.password');
          $str = 'name ="'.$name. '" and tel = "'.$psd.'"';
          var_dump($str);
          $row = M('user')->where($str)->find();
          if($row)
            $this->redirect("Index/index");
          else
            $this->redirect('login','',1,'用户名或密码错误');
        }
        else{
          $this->redirect('login','',1,'验证码错误');
        }
      }
      $this->display();
    }
    public function verifyImg(){
      //设置验证码的宽高字体大小以及验证码的个数,设计其他的参照Think\Verify里面的设置
      $config=array(
        'imageW'  => 150,
        'imageH'  => 40,
        'fontSize' => 20,
        'length'  => 4
      );
      $obj = new \Think\Verify($config);
      $obj->entry();
    }
  }

表单部分

<form action="login" method="post">
  <table valign="top" width="50%">
 <tr><td colspan="2"><h4 style="letter-spacing:1px;font-size:16px;">RainMan 网站管理后台</h4></td></tr>
 <tr><td>管理员:</td><td><input type="text" name="user_name" value="" /></td></tr>
 <tr><td>密    码:</td><td><input type="password" name="password" value="" /></td></tr>
 <tr><td>验证码:</td>
   <td><input type="text" name="yanzhengma" value="" style="width:80px;"/></td>
   <td><img src="__URL__/verifyImg" onclick="this.src='__URL__/verifyImg/'+Math.random()" alt=""/></td>
 </tr>
 <tr class="bt" align="center"><td> <input type="submit" value="登陆" /></td><td> <input type="reset" value="重填" /></td></tr>
  </table>
</form>

相关推荐:

thinkPHP实现多字段模糊匹配查询的方法

thinkphp实现文件上传和文件下载


以上就是thinkPHP实现的验证码登录功能的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。