【php:thinkphp】下获取验证码

最近写验证码,总结下Thinkphp下验证码:

我们在thinkphp框架写验证码当然少不控制器和视图

1.控制器,控制器是路径为Application/Home/Controller,下边贴控制器代码:

<span style="font-size:18px;"><?php
namespace Home\Controller;
use Think\Controller;
class ChongController extends Controller {
    //index
    public function index(){
        
        $this->display(index);

    }
    //生成验证码
   public function verify(){  
    $Verify = new \Think\Verify();  
    $Verify->fontSize = 18;  
    $Verify->length   = 4;  
    $Verify->useNoise = false;  
    $Verify->codeSet = '0123456789';  
    $Verify->imageW = 130;  
    $Verify->imageH = 50;  
    //$Verify->expire = 600;  
    $Verify->entry();  
    //$this->display();
}
}

?></span>
请无视我捉急的英语,Verification这个单词需要翻一下所以命名就随便谢谢了。

控制器继承/Think/Controller控制器。主要有两个函数,一个为默认index,另一个是生成验证码图片函数verify.

我们通过访问servername/home/chong可以访问到我们站点下app/Home/Controller/chongController.class.php的index 函数,index函数的display()会调用对应的index.html.

2.然后就是我们的index.html出场了,他的路径为App/Home/View/Chong目录下,一下为index.html的代码:

<span style="font-size:18px;"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
  <p class="top15 captcha" id="captcha-container" style="background-color: red">  
  <input name="verify" width="50%" height="50" class="captcha-text" placeholder="验证码" type="text">                  
  <img width="30%" class="left15" height="50" alt="验证码" src="{:U('Home/Chong/verify',array())}" title="点击刷新" οnclick="this.src=this.src+'?t='+Math.random()">  
</p>
</body><span style="font-family: Arial, Helvetica, sans-serif;"></html></span></span>
大致就是写一个img标签 scr为看代码吧。然后如果想刷新的话可以加上在img 标签上加上 οnclick="this.src=this.src+'?t='+Math.random()"就可以了。

3.验证验证码

在Application/Home/Controller的ChongController.class.php下添加函数

<span style="font-size:18px;">public function check(){
    $verify = I('param.verify','');
    $check=new\Think\Verify();
if(!$check->check($verify)){  
    $this->error("亲,验证码输错了哦!",$this->site_url,9);  
} 
}</span>
在这里可以使用ajax方式提交,也可以使用form表格方式提交验证。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值