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 相关文章推荐
如何在PHP中进行身份认证
Oct 09 PHP
php中设置多级目录session的问题
Aug 08 PHP
PHP 实现explort() 功能的详解
Jun 20 PHP
PHP中的静态变量及static静态变量使用详解
Nov 05 PHP
php代码架构的八点注意事项
Jan 25 PHP
Yii2框架使用计划任务的方法
May 25 PHP
PHP使用Redis替代文件存储Session的方法
Feb 15 PHP
PHP设计模式之模板模式定义与用法详解
Dec 20 PHP
Yii 使用intervention/image拓展实现图像处理功能
Jun 22 PHP
PHP实现cookie跨域session共享的方法分析
Aug 23 PHP
laravel利用中间件防止未登录用户直接访问后台的方法
Sep 30 PHP
php TP5框架生成二维码链接
Apr 01 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
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
2020/04/05 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
php获取用户真实IP和防刷机制的实例代码
2018/11/28 PHP
Hutia 的 JS 代码集
2006/10/24 Javascript
使用JavaScript修改浏览器URL地址栏的实现代码
2013/10/21 Javascript
详解jQuery插件开发中的extend方法
2013/11/19 Javascript
javascript 模拟坦克大战游戏(html5版)附源码下载
2014/04/08 Javascript
angularjs表格分页功能详解
2016/01/21 Javascript
基于jQuery实现带动画效果超炫酷的弹出对话框(附源码下载)
2016/02/22 Javascript
JavaScript实现iframe自动高度调整和不同主域名跨域
2016/02/27 Javascript
遍历json获得数据的几种方法小结
2017/01/21 Javascript
JS脚本实现网页自动秒杀点击
2018/01/11 Javascript
JavaScript引用类型Array实例分析
2018/07/24 Javascript
vuejs2.0运用原生js实现简单拖拽元素功能
2020/08/21 Javascript
react koa rematch 如何打造一套服务端渲染架子
2019/06/26 Javascript
jQuery 查找元素操作实例小结
2019/10/02 jQuery
Python实现简单HTML表格解析的方法
2015/06/15 Python
判断网页编码的方法python版
2016/08/12 Python
python 用opencv调用训练好的模型进行识别的方法
2018/12/07 Python
python爬取内容存入Excel实例
2019/02/20 Python
python仿evething的文件搜索器实例代码
2019/05/13 Python
python绘制地震散点图
2019/06/18 Python
梅尔频率倒谱系数(mfcc)及Python实现
2019/06/18 Python
详解python中__name__的意义以及作用
2019/08/07 Python
Python产生一个数值范围内的不重复的随机数的实现方法
2019/08/21 Python
巴西家用小家电购物网站:Polishop
2016/08/07 全球购物
Vilebrequin美国官方网上商店:法国豪华泳装品牌
2020/02/22 全球购物
史上最全面的Java面试题汇总!
2015/02/03 面试题
职业生涯规划书基本格式
2014/01/06 职场文书
关于赌博的检讨书
2014/01/24 职场文书
安全教育实施方案
2014/03/02 职场文书
中药学专业毕业生推荐信
2014/07/10 职场文书
签证工作证明模板
2015/06/15 职场文书
体育教师教学随笔
2015/08/15 职场文书
2016年度基层党建工作公开承诺书
2016/03/25 职场文书
springboot创建的web项目整合Quartz框架的项目实践
2022/06/21 Java/Android