当前位置: 移动技术网 > IT编程>开发语言>PHP > thinkPHP实现的验证码登录功能示例

thinkPHP实现的验证码登录功能示例

2017年11月02日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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模板操作技巧总结》、《thinkphp常用方法总结》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《zend framework框架入门教程》及《php模板技术总结》。

希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网