关于tp3.2.3验证码简单使用

在Application/Admin/View/Index下添加index.html

在index.html里把表单写出来

 


编辑IndexController.class.php显示验证码图片

在index.php里面添加<img>验证码图片的地址使用U函数生成,用于页面显示验证码图片

 

在index.html中实现点击验证码图片切换的两种方法

1、使用JQuery实现验证码的切换(在public下js里面装有jQuery文件),写title能让使用者更加清楚

2、直接在<img>里面添加onclick


当表单index.html编写完毕,此时编写服务端处理逻辑,编辑IndexController.class.php,添加check方法,主要是验证验证码是否正确

验证码正确样式:


验证码错误样式:

代码如下:

IndexController.class.php:

<?php

namespace Admin\Controller;

use Think\Controller;

class IndexController extends Controller {

    public function index(){

        $this->display();

    }

 

    public function verify(){

            $Verify = new \Think\Verify();

       $Verify->length   = 4;   //验证码个数

                 $Verify->entry(1);

    }

   

    public function check(){

            header("Content-type: text/html; charset=utf-8");

            $code=I('code');

              $verify = new\Think\Verify();

            if($verify->check($code, 1)){

          $this->success("验证码正确") ;

            }else{

                     $this->error("验证码错误");

            }

    }

}

 

 

 

Index.html:

<!DOCTYPE html>

<html lang="en">

<head>

        <meta charset="UTF-8">

        <title></title>

        <scriptsrc="__PUBLIC__/js/jquery-1.6.2.min.js"></script>

        </script>

        <script>

                 $(function(){

          $('img').click(function(){

               $('img').attr('src',"__URL__/verify/random/"+Math.random());//点击事件改变图片地址

           });

                 });

        </script>

</head>

<body>

        <formmethod="post" action="__URL__/check">

                 <label>用户名:</label><inputtype="text" name="username"><br><br>

                 <label>密码:</label><inputtype="password" name="password"><br><br>

                 <label>验证码:</label><inputtype="text" name="code"><br><br>

                 <imgsrc="{:U('Index/verify')}" title="看不清,换一张"><br><br>

                 <inputtype="submit" name="登录">

        </form>

</body>

</html>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值