ThinkPHP中自带能生成验证码的类:ThinkPHP/Library/Think/Verify.class.php

默认情况下,验证码的字体是随机使用 ThinkPHP/Library/Think/Verify/ttfs/目录下面的字体文件,我们可以指定验证码的字体

汉字的验证码:ThinkPHP/Library/Think/Verify/zhttfs/添加中文的字体格式

更改字体:ttf格式

6月19  使用tp框架生成验证码及文件上传

 

6月19  使用tp框架生成验证码及文件上传

6月19  使用tp框架生成验证码及文件上传

1.例题:通过验证码实现用户的登录,并利用jquery实现点击图片验证码进行新的刷新

LoginController.class.php(Login方法和yzm方法)

<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller
{
    public function Login()
    {
        if(empty($_POST))
        {
            $this->display();    
        }    
        else
        {
            //判断验证码是否正确
            $code = $_POST["yzm"];
            $verify = new \Think\Verify();  
            if($verify->check($code))
            {
                if($_POST["uid"]!="")
                {
                    $model = D("users");        
        
                    $uid = $_POST["uid"];
                    $pwd = $_POST["pwd"];
                    
                    $attr = $model->field("Pwd")->find($uid);
                    //echo $attr["pwd"];
                    
                    if($pwd == $attr["pwd"])
                    {
                        session("uid",$uid);
                        $this->success("登录成功","Main");
                    }
                    else
                    {
                        $this->error("登录失败");    
                    }
                }
                else
                {
                    $this->error("登录失败");    
                }
            
            }
            else
            {
                $this->error("验证码错误");    
            }
        }
    }
    
    //生成验证码的操作
    public function yzm()
    {
        $config =    array(   
        'fontSize'    =>    30,    // 验证码字体大小    
        //'length'      =>    3,     // 验证码位数 
        //'useNoise'    =>    false, // 关闭验证码杂点
        'imageW'  => 300,
        'imageH'  => 90,
        'useZh' => true,
        'fontttf' => 'STXINWEI.TTF',
        );
         
        $Verify = new \Think\Verify($config);
        //$Verify->fontttf = '7.ttf';  // 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
        $Verify->entry();    
    }
}
View Code

相关文章: