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文件向另一个地址post数据,不用表单和隐藏的变量的
Mar 06 PHP
Win2003服务器安全加固设置--进一步提高服务器安全性
May 23 PHP
php mssql 数据库分页SQL语句
Dec 16 PHP
PHP遍历二维数组的代码
Apr 22 PHP
php中return的用法实例分析
Feb 28 PHP
浅谈PDO的rowCount函数
Jun 18 PHP
Smarty foreach控制循环次数的一些方法
Jul 01 PHP
PHP使用redis实现统计缓存mysql压力的方法
Nov 14 PHP
CodeIgniter视图使用注意事项
Jan 20 PHP
Yii2实现ajax上传图片插件用法
Apr 28 PHP
Yii2第三方类库插件Imagine的安装和使用
Jul 06 PHP
Laravel框架实现文件上传的方法分析
Sep 29 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
《神奇女侠:血脉》神力女超人大战犯罪公司
2020/04/09 欧美动漫
用PHP和MySQL保存和输出图片
2006/10/09 PHP
超强分页类2.0发布,支持自定义风格,默认4种显示模式
2007/01/02 PHP
Drupal7中常用的数据库操作实例
2014/03/02 PHP
ThinkPHP3.1新特性之命名范围的使用
2014/06/19 PHP
php使用curl打开https网站的方法
2015/06/17 PHP
js父页面与子页面不同时显示的方法
2014/10/16 Javascript
原生javascript实现Tab选项卡切换功能
2015/01/12 Javascript
遮罩层点击按钮弹出并且具有拖动和关闭效果(两种方法)
2015/08/20 Javascript
Jquery ajax基础教程
2015/11/20 Javascript
分享有关jQuery中animate、slide、fade等动画的连续触发、滞后反复执行的bug
2016/01/10 Javascript
JavaScript获取客户端IP的方法(新方法)
2016/03/11 Javascript
jQuery操作属性和样式详解
2016/04/13 Javascript
javascript实现无法关闭的弹框
2016/11/27 Javascript
JS中parseInt()和map()用法分析
2016/12/16 Javascript
vue.js 左侧二级菜单显示与隐藏切换的实例代码
2017/05/23 Javascript
Vuex 使用 v-model 配合 state的方法
2018/11/13 Javascript
Jquery $.map使用方法实例详解
2020/09/01 jQuery
Python读写Redis数据库操作示例
2014/03/18 Python
python脚本实现xls(xlsx)转成csv
2016/04/10 Python
python中字符串类型json操作的注意事项
2017/05/02 Python
Python reduce()函数的用法小结
2017/11/15 Python
python编程通过蒙特卡洛法计算定积分详解
2017/12/13 Python
Python之列表的插入&amp;替换修改方法
2018/06/28 Python
python3结合openpyxl库实现excel操作的实例代码
2018/09/11 Python
对python中数组的del,remove,pop区别详解
2018/11/07 Python
Flask模板引擎之Jinja2语法介绍
2019/06/26 Python
Python+Tensorflow+CNN实现车牌识别的示例代码
2019/10/11 Python
一款利用css3的鼠标经过动画显示详情特效的实例教程
2014/12/29 HTML / CSS
Html5实现单张、多张图片上传功能
2019/04/28 HTML / CSS
英国足球店:UK Soccer Shop
2017/11/19 全球购物
求职信结尾怎么写
2014/05/26 职场文书
战略性融资合作协议书范本
2014/10/17 职场文书
2014年妇女工作总结
2014/12/06 职场文书
Win11应用商店打开闪退怎么解决? win11应用商店打不开的多种解决办法
2022/04/05 数码科技
Java使用HttpClient实现文件下载
2022/08/14 Java/Android