PHP 验证码类的实现

本文介绍了PHP验证码的基本原理,包括服务器生成随机字符串并保存在Session,与客户端提交的验证码进行比对。同时,详细讲解了GD库相关函数,如imagecreate、imagecolorallocate等,并展示了PHP验证码类的代码实现及具体使用方法,帮助理解验证码的生成与验证过程。
摘要由CSDN通过智能技术生成

验证码的基本原理:

  • 服务器生成随机字符串,保存在Session中,并将验证码图片返回给客户端。
  • 用户根据图片填写验证码并提交。
  • 将用户填写的验证码和Session中的验证码进行对比,一致通过,不一致不通过。

使用PHP编写验证码的实现类

  • 开启PHP的GD模块的支持
    在php.ini中:
    开启GD扩展
    检查是否开启(如图所示表示开启):
    检查GD库开启

相关函数的解释

  • imagecreate($width, $height); //创建画布

    $width 画布的宽度。
    $height 画布的高度。

  • imagecolorallocate( $resource, $red, $green, $blue); //分配颜色

    $resource 图像资源对象。
    $red, $green, $blue 颜色热rgb值。 比如(255,255,255) 代表白色

  • imagefill( $resource, $x, $y, $color ); //填充颜色

    $x x轴的位置
    $y y轴的位置
    $color 颜色的码值

  • imagestring( $resource, $fontSize, $x, $y, $string, $color ); //添加字符串

    $fontSize 字体的字号
    $x, $y 坐标的位置
    $string 要绘制的字符串
    $color $color字体的颜色

  • imageline( $resource, $x1, $y1, $x2, $y2, $color ); //绘制线段

    $x1, $y1, $x2, $y2 坐标的位置
    $color 线段的颜色

  • imagesetpixel( $resource, $x, $y, $color ); //指定的坐标处绘制一个像素

    $x, $y 坐标的位置
    点的颜色

  • imagejpeg( $resource, $path ); imagepng( $resource, $path ); imagegif( $resource, $path );

    $resource 图像资源对象
    $path 要保存的路径
    $path 不写,表示显示图片,需要加上header。 $path写,表示保存图片,一般不加header

  • imagedestroy($resource);

  • imagecreatefromjpeg( $path ); imagecreatefrompng( $path ); imagecreatefromgif( $path );

    以文件或者URL加载图像资源

PHP验证码类的代码实现:

<?php
namespace Tools;
class VerifyCode {
    private $charList = 'QWERTYUIPASDFGHJKLZXCVBNM123456789';// 可能出现的字符
    private $codeLen = 4; // 验证码的长度
    private $fontSize = 5; // 验证码字体的大小
    private $width = 120; // 验证码图像的宽带
    private $height = 20; // 验证码图像的高度
    private $charLen = 0;
    private $resource = null; // 图像资源对象
    private $pointCount = 200; // 点的数量
    private $lineCount = 4; // 线的数量

    public function __construct() {
        $this->charLen = strlen($this->charList);
    }

    public function setCodeLen($codeLen) {
        $this->codeLen = $codeLen;
    }

    public function setFontSize($fontSize) {
        $this->fontSize = $fontSize;
    }

    public function setWidth($width) {
        $this->width = $width;
    }

    public function setHeight($height) {
        $this->height = $height;
    }

    public function setPointCount($pointCount) {
        $this->pointCount = $pointCount;
    }

    public function setLineCount($lineCount) {
        $this->lineCount = $lineCount;
    }
    
    public function draw() {
        // 创建图像资源(画布)
        $this->resource = imagecreate($this->width,$this->height);
        // 设置背景颜色
        $background = imagecolorallocate($this->resource,255,255,255);
        // 填充背景颜色
        imagefill($this->resource,0,0,$background);
        // 添加码值
        $this->generateCode();
        header('content-type:image/jpeg');
        // 画点
        $this->drawPoint();
        // 画线
        $this->drawLine();
        imagejpeg($this->resource);
        imagedestroy($this->resource);
    }

    /**
     * 画干扰线,随机颜色
     */
    public function drawLine() {
        for ($i=0; $i < $this->lineCount; $i++) { 
            $x1 = mt_rand(1,$this->width);
            $y1 = mt_rand(1,$this->height);
            $x2 = $x1 + mt_rand(($this->height / 4),($this->height * 2));
            $y2 = $y1 + mt_rand(($this->height / 4),($this->height / 2));
            $linecolor = imagecolorallocate($this->resource,rand(70,200),rand(70,200),rand(70,200));
            imageline($this->resource,$x1,$y1,$x2,$y2,$linecolor);
        }
    }

    /**
     * 画干扰点,随机颜色
     */
    public function drawPoint() {
        for ($i=0; $i < $this->pointCount; $i++) { 
            $pointcolor = imagecolorallocate($this->resource,rand(70,200),rand(70,200),rand(70,200));
            imagesetpixel($this->resource, rand(1,$this->width), rand(1,$this->height), $pointcolor);
        }
    }

    /**
     * 生成码值
     */
    public function generateCode() {
        $code = '';
        $fontH = imagefontheight($this->fontSize);
        $imageH = imagesy($this->resource);
        $fontW = imagefontwidth($this->fontSize);
        $imageW = imagesx($this->resource);
        $W = ($imageW - $fontW * $this->codeLen) / 2;
        $H = ($imageH - $fontH) / 2;
        for($i = 0;$i < $this->codeLen;$i++) {
            $charIndex = mt_rand(0,$this->charLen-1);
            // 随机获取字符
            $char = $this->charList[$charIndex];
            $code .= $char;
            // 随机分配字体颜色
            $fontColor = imagecolorallocate($this->resource,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
            $x = (($this->width - $W) / $this->codeLen * $i) + mt_rand(5,10)+$W/2;
            $y = mt_rand(1,$H);
            // 画布上添加字符
            imagestring($this->resource,$this->fontSize,$x,$y,$char,$fontColor);
        }
        @session_start();
        $_SESSION['verifyCode'] = $code; 
    }

    /**
     * 验证验证码是否正确
     */
    public static function checkCode($code) {
        @session_start();
        return $code == $_SESSION['verifyCode'];
    }
}
?>

具体使用

<?php
include_once './tools/VerifyCode.php';
use Tools\VerifyCode;
$verify = new VerifyCode();
$verify->setWidth(150); // 可以不进行设置,使用默认值
$verify->setHeight(30); // 可以不进行设置,使用默认值
$verify->setFontSize(6); // 可以不进行设置,使用默认值
$verify->setCodeLen(5); // 可以不进行设置,使用默认值
$verify->setLineCount(4); // 可以不进行设置,使用默认值
$verify->setPointCount(180); // 可以不进行设置,使用默认值
$verify->draw();
?>

效果图:
验证码效果图

验证码的校验:

<?php
include_once './tools/VerifyCode.php';
use Tools\VerifyCode;
$code = '5PVTS'; 
// $code需要从用户输入的表单中获取。($_POST['表单的验证码名'])
if(VerifyCode::checkCode($code)) {
    echo '验证码正确';
}
else{
    echo '验证码不正确';
}
?>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值