php生成验证码,并完成后台验证

1.需要用到的知识

1.随机验证码文本的生成。
2.通过 gd库生成图片

2.随机验证码文本的生成

这部分比较简单,直接上代码。

//随机生成二维码
    private function rand_code()
    {
        $code = "";
        $txt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for ($i = 0; $i < $this->code_num; $i++) {
            $code.= $txt[mt_rand(0, strlen($txt) - 1)];
        }
        return $code;
    }

这里生成的是大小写字母和数字的二维码,如果要有中文则稍微复杂一点。

3.验证码图片的生成

验证码的生成主要分为几个部分:

  1. 创建画布对象;
  2. 为画布设置干扰线条;
  3. 为画布设置干扰像素点;
  4. 将生成的文本验证码绘制到画布中。

(1)创建画布

//创建二维码背景
    private function create_backgroud_image()
    {
        //绘制画布
        $this->res = imagecreatetruecolor($this->width, $this->height);
        //设置背景颜色
        $background_color = imagecolorallocate($this->res, 200, 200, 200);
        //为画布填充背景色
        imagefill($this->res, 0, 0, $background_color);
    }

(2)绘制干扰线条

//为二维码背景生成随机线条
    private function create_line()
    {

        imagesetthickness($this->res,$this->create_line_width());
        for ($i = 0; $i < 10; $i++) {
            imageline($this->res, 
                      mt_rand(0, $this->width), 
                      mt_rand(0, $this->height), 
                      mt_rand(0, $this->width), 
                      mt_rand(0, $this->height), 
                      $this->create_color(150,255,150,255,150,255));
        }
    }

(3)绘制干扰像素点

//为二维码背景生成随机点
   private function create_spot(){
       $max = $this->width * 5;
       for($i=0;$i<$max;$i++){
           imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->create_color(100,255));
       }
   }

(4)将生成的文本验证码绘制到画布中

//生成验证码图片
    private function create_txt_image($txt){
        //$txt = $this->rand_code();//获取生成的code        
        $font = realpath("times.TTF");
        $size = $this->width/$this->code_num;
        $w = $size<$this->width?$size:$this->width;
        for($i=0;$i<$this->code_num;$i++){
            $txt_color = imagecolorallocate($this->res,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
                                            imagettftext($this->res,
                                            $w,
                                            mt_rand(-10,10),
                                            ($w)*$i,
                                            $this->height/1.4,
                                            $this->create_color(0,130,0,130,0,130),
                                            $font,
                                            $txt[$i]);
        }
        
    }

将上述方法整合到Captcha类中,代码如下

<?php

/**
 * 验证码生成类
 */
class Captcha
{
    // protected $code = "";
    //对外
    protected $width;
    protected $height;
    protected $res;
    protected $code_num;
    public function __construct($width = 100,$height = 50,$code_num = 5)
    {
        $this->width = $width;
        $this->height = $height;
        $this->code_num = $code_num;
    }
    public function render()
    {
        header("Content-type:image/jpg");
        $this->create_backgroud_image();
        $this->create_line();
        $this->create_spot();
        $code = $this->rand_code();
        $this->create_txt_image($code);
        imagepng($this->res);
        imagedestroy($this->res);
        return $code;
    }
    //随机生成验证码
    private function rand_code()
    {
        $code = "";
        $txt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for ($i = 0; $i < $this->code_num; $i++) {
            $code.= $txt[mt_rand(0, strlen($txt) - 1)];
        }
        return $code;
    }
    //创建二维码背景
    private function create_backgroud_image()
    {
        //绘制画布
        $this->res = imagecreatetruecolor($this->width, $this->height);
        //设置背景颜色
        $background_color = imagecolorallocate($this->res, 200, 200, 200);
        //为画布填充背景色
        imagefill($this->res, 0, 0, $background_color);
    }
    //生成随机颜色
    private function create_color($min_r = 0,$max_r = 255,$min_g = 0,$max_g = 255,$min_b = 0,$max_b = 255)
    {
        return imagecolorallocate($this->res, mt_rand($min_r,$max_r), mt_rand($min_g,$max_g), mt_rand($min_b,$max_b));
    }
    //自动生成线条的宽度
    private function create_line_width(){
        $w = $this->width/100;
        return $w;
    }
    //为验证码背景生成随机线条
    private function create_line()
    {

        imagesetthickness($this->res,$this->create_line_width());
        for ($i = 0; $i < 10; $i++) {
            imageline($this->res, 
                      mt_rand(0, $this->width), 
                      mt_rand(0, $this->height), 
                      mt_rand(0, $this->width), 
                      mt_rand(0, $this->height), 
                      $this->create_color(150,255,150,255,150,255));
        }
    }
    //为验证码背景生成随机点
    private function create_spot(){
        $max = $this->width * 5;
        for($i=0;$i<$max;$i++){
            imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->create_color(100,255));
        }
    }
    //生成验证码图片
    private function create_txt_image($txt){
        //$txt = $this->rand_code();//获取生成的code        
        $font = realpath("times.TTF");
        $size = $this->width/$this->code_num;
        $w = $size<$this->width?$size:$this->width;
        for($i=0;$i<$this->code_num;$i++){
            $txt_color = imagecolorallocate($this->res,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
                                            imagettftext($this->res,
                                            $w,
                                            mt_rand(-10,10),
                                            ($w)*$i,
                                            $this->height/1.4,
                                            $this->create_color(0,130,0,130,0,130),
                                            $font,
                                            $txt[$i]);
        }
        
    }
}

下面是项目结构
项目结构

1.php代码

<?php
session_start();
include_once "Captcha.php";
header("Content-type:image/jpg");
$captcha = new Captcha(100,50);
$code = $captcha->render();
$_SESSION["captcha"] = $code;

1.html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>    
    <form action="2.php" method="POST">
        <input id="captcha" name="captcha" type="text" placeholder="请输入验证码">
        <img src="1.php" alt="验证码" onclick="this.src='1.php?'+Math.random()" />
        <input type="submit" value="提交">
    </form>
</body>
<script>
    
</script>
</html>

2.php代码

<?php
session_start();
$captcha = strtolower($_POST["captcha"]);
if($captcha ==strtolower($_SESSION["captcha"])){
    echo "ok";//验证码成功
}else{
    echo "fail";//验证码失败
}

*.ttf为字体文件

项目代码在这里:https://download.csdn.net/download/weixin_38017811/19577265?spm=1001.2014.3001.5501

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值