PHP code 验证码生成类定义和简单使用示例

 更新时间:2020年05月27日 09:08:58   作者:人生如初见_张默  
这篇文章主要介绍了PHP code 验证码生成类定义和简单使用,结合实例形式分析了PHP code 验证码生成类的基本功能定义、简单使用方法及操作注意事项,需要的朋友可以参考下

本文实例讲述了PHP code 验证码生成类定义和简单使用。分享给大家供大家参考,具体如下:

code.php

<?php
namespace code;
/**
 * Class Code
 */
class Code
{
  protected $number;//验证码内字符个数
  protected $codeType;//验证码样式
  protected $width;//图像宽
  protected $height;//图像高
  protected $code;//验证码
  protected $image;//图像资源
 
  /**
   * Code constructor.
   * @param int $number
   * @param int $codeType
   * @param int $width
   * @param int $height
   */
  public function __construct($number=5, $codeType=2, $width=100, $height=40)
  {
    $this->number = $number;
    $this->codeType = $codeType;
    $this->width = $width;
    $this->height = $height;
    $this->code = $this->createCode();
  }
 
  /**
   * 销毁资源
   */
  public function __destruct()
  {
    imagedestroy($this->image);
  }
 
  /**
   * 外部调用code时触发
   * @param $name
   * @return bool
   */
  public function __get($name)
  {
    if ('code' == $name) {
      return $this->$name;
    } else {
      return false;
    }
  }
 
  /**
   * 生成code
   */
  protected function createCode()
  {
    switch ($this->codeType) {
      case 0:
        $code = $this->getNum();
        break;
      case 1:
        $code = $this->getChar();
        break;
      case 2:
        $code = $this->getNumChar();
        break;
      default:
        die('样式不对');
    }
    return $code;
  }
 
  /**
   * 数字验证码
   * @return string
   */
  protected function getNum()
  {
    $str = join('', range(0,9));
    return substr(str_shuffle($str), 0, $this->number);
  }
 
  /**
   * 字符验证码
   * @return string
   */
  protected function getChar()
  {
    $str = join('', range('a', 'z'));
    $str = $str . strtoupper($str);
    return substr(str_shuffle($str), 0, $this->number);
  }
 
  /**
   * 字符和数字混合验证码
   * @return string
   */
  protected function getNumChar()
  {
    $num = join('', range(0, 9));
    $str = join('', range('a', 'z'));
    $str_big = strtoupper($str);
    $numChar = $num . $str . $str_big;
    return substr(str_shuffle($numChar), 0, $this->number);
  }
 
  /**
   * 生成图像
   */
  protected function createImage()
  {
    $this->image = imagecreatetruecolor($this->width, $this->height);
  }
 
  /**
   * 填充背景色
   */
  protected function fillColor()
  {
    imagefill($this->image, 0, 0, $this->lightColor());
  }
 
  /**
   * 浅颜色
   * @return int
   */
  protected function lightColor()
  {
    return imagecolorallocate($this->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255));
  }
 
  /**
   * 深颜色
   * @return int
   */
  protected function darkColor()
  {
    return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
  }
 
  /**
   * 添加验证码字符
   */
  protected function drawChar()
  {
    $width = ceil($this->width/$this->number);
    for ($i = 0; $i < $this->number; $i++) {
      $x = mt_rand($i * ($width - 5), ($i + 1) * ($width - 5));
      $y = mt_rand(0, $this->height - 15);
      imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
    }
  }
 
  /**
   * 添加干扰点
   */
  protected function drawDisturb()
  {
    for ($i= 0; $i < 100; $i++) {
      imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor());
    }
  }
 
  /**
   * 添加干扰线
   */
  protected function drawArc()
  {
    for ($i = 0; $i < $this->number - 3; $i++) {
      imagearc($this->image, mt_rand(5, $this->width), mt_rand(5, $this->height), mt_rand(5, $this->width), mt_rand(5, $this->height),mt_rand(0, 70), mt_rand(300, 360), $this->darkColor());
    }
  }
 
  /**
   * 输出显示
   */
  protected function show()
  {
    header('Content-Type:image/png');
    imagepng($this->image);
  }
 
  /**
   * 外部image
   */
  public function outImage()
  {
    $this->createImage();//创建画布
    $this->fillColor();//填充背景色
    $this->drawChar();//添加验证字符
    $this->drawDisturb();//添加干扰点
    $this->drawArc();//添加干扰线
    $this->show();//输出
  }
}

展示验证码。。保存验证码和过期时间

<?php
include './code/Code.php';
 
$code = new code\Code();
$code->outImage();
session_start();
$_SESSION['code'] = [
  'code' => $code->code,
  'exp_time' => time() + (60 * 60 * 10),
];

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

  • PHP实现懒加载的方法

    PHP实现懒加载的方法

    这篇文章主要介绍了PHP实现懒加载的方法,实例分析了php加载的原理与懒加载的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • PHP数组操作——获取数组最后一个值的方法

    PHP数组操作——获取数组最后一个值的方法

    这篇文章主要介绍了PHP数组操作——获取数组最后一个值的方法,需要的朋友可以参考下
    2015-04-04
  • php经典算法集锦

    php经典算法集锦

    这篇文章主要介绍了php经典算法,实例分析了汉诺塔、排序、查找、递归等算法技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • PHP 时间转换Unix时间戳代码

    PHP 时间转换Unix时间戳代码

    PHP 时间转换Unix 时间戳实现代码。
    2010-01-01
  • 详解PHP设计模式之依赖注入模式

    详解PHP设计模式之依赖注入模式

    依赖注入模式:依赖注入是控制反转的一种实现方式。要实现控制反转,通常的解决方案是将创建被调用者实例的工作交由 IoC 容器来完成,然后在调用者中注入被调用者(通过构造器 / 方法注入实现),这样我们就实现了调用者与被调用者的解耦,该过程被称为依赖注入。
    2021-05-05
  • php服务器的系统详解

    php服务器的系统详解

    在本篇文章里小编给大家整理的是关于php服务器用什么系统的相关知识点内容,有兴趣的朋友们跟着学习参考下。
    2019-10-10
  • PHP session反序列化漏洞超详细讲解

    PHP session反序列化漏洞超详细讲解

    这篇文章主要介绍了PHP session反序列化漏洞,php session反序列化漏洞存在的原因是当序列化session和读取反序列化字符时采用的序列化选择器不一样时,处理的方法不一样
    2023-02-02
  • php自动注册登录验证机制实现代码

    php自动注册登录验证机制实现代码

    在phpwind站点后台添加一个名为“广告管家”(广告管家为CNZZ的一款广告投放的应用)的应用,整个“广告管家”的应用是通过iframe载入,载入的具体内容根据不同站点显示针对该站点的具体内容
    2011-12-12
  • 控制PHP的输出:缓存并压缩动态页面

    控制PHP的输出:缓存并压缩动态页面

    PHP4中最令人满意的事是——你可以让PHP缓存所有由脚本生成的输出,在你决定把它们送出之前,浏览器方是不会收到任何内容的
    2013-06-06
  • php实现图片局部打马赛克的方法

    php实现图片局部打马赛克的方法

    这篇文章主要介绍了php实现图片局部打马赛克的方法,实例分析了php针对图片操作的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-02-02

最新评论