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


Posted in PHP onMay 27, 2020

本文实例讲述了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 相关文章推荐
不用数据库的多用户文件自由上传投票系统(3)
Oct 09 PHP
PHP中ADODB类详解
Mar 25 PHP
php分页思路以及在ZF中的使用
May 30 PHP
php编写的简单页面跳转功能实现代码
Nov 27 PHP
php中运用http调用的GET和POST方法示例
Sep 29 PHP
dedecms中使用php语句指南
Nov 13 PHP
浅析php创建者模式
Nov 25 PHP
迪菲-赫尔曼密钥交换(Diffie?Hellman)算法原理和PHP实现版
May 12 PHP
php类的扩展和继承用法实例
Jun 20 PHP
搭建Vim为自定义的PHP开发工具的一些技巧
Dec 11 PHP
php导出csv文件,可导出前导0实例代码
Nov 16 PHP
laravel实现按月或天或小时统计mysql数据的方法
Oct 09 PHP
PHP 计算至少是其他数字两倍的最大数的实现代码
May 26 #PHP
tp5.1 框架数据库-数据集操作实例分析
May 26 #PHP
tp5.1 框架路由操作-URL生成实例分析
May 26 #PHP
tp5.1 框架join方法用法实例分析
May 26 #PHP
tp5.1框架数据库子查询操作实例分析
May 26 #PHP
tp5.1 框架数据库常见操作详解【添加、删除、更新、查询】
May 26 #PHP
Laravel 修改验证异常的响应格式实例代码详解
May 25 #PHP
You might like
最小化数据传输――在客户端存储数据
2006/10/09 PHP
第四节--构造函数和析构函数
2006/11/16 PHP
php下目前为目最全的CURL中文说明
2010/08/01 PHP
PHP获取http请求的头信息实现步骤
2012/12/16 PHP
php下载文件源代码(强制任意文件格式下载)
2014/05/09 PHP
PHP SPL标准库中的常用函数介绍
2015/05/11 PHP
深入浅析php json 格式控制
2015/12/24 PHP
PHP使用token防止表单重复提交的方法
2016/04/07 PHP
用JavaScript玩转游戏物理(一)运动学模拟与粒子系统
2010/06/19 Javascript
js简单实现用户注册信息的校验代码
2013/11/15 Javascript
JS 数字转换研究总结
2013/12/26 Javascript
JavaScript获取当前cpu使用率的方法
2015/12/15 Javascript
详解JavaScript的AngularJS框架中的作用域与数据绑定
2016/03/04 Javascript
浅谈javascript中执行环境(作用域)与作用域链
2016/12/08 Javascript
vue-resource 拦截器使用详解
2017/02/21 Javascript
给Easyui-Datebox设置隐藏或者不可用的解决方法
2017/05/26 Javascript
vue-cli单页应用改成多页应用配置详解
2017/07/14 Javascript
JS闭包的几种常见形式实例详解
2017/09/16 Javascript
vue构建动态表单的方法示例
2018/09/22 Javascript
解决一个微信号同时支持多个环境网页授权问题
2019/08/07 Javascript
jQuery Datatables 动态列+跨列合并实现代码
2020/01/30 jQuery
原生JS实现相邻月份日历
2020/10/13 Javascript
python实现根据图标提取分类应用程序实例
2014/09/28 Python
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
2017/06/12 Python
Python中三元表达式的几种写法介绍
2019/03/04 Python
PyQt4编程之让状态栏显示信息的方法
2019/06/18 Python
python中wx模块的具体使用方法
2020/05/15 Python
意大利文具和办公产品在线商店:Y-Office
2020/02/27 全球购物
应届毕业生如何写求职信
2014/02/16 职场文书
日语专业求职信
2014/07/04 职场文书
入党积极分子批评与自我批评思想汇报
2014/09/14 职场文书
医生见习报告范文
2014/11/03 职场文书
家庭财产分割协议书范本
2014/11/24 职场文书
mybatis 获取无数据的字段不显示的问题
2021/07/15 Java/Android
python字典的元素访问实例详解
2021/07/21 Python
「天才王子的赤字国家重生术」妮妮姆·拉雷粘土人开订
2022/03/21 日漫