亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP code 驗證碼生成類定義和簡單使用示例

 更新時間:2020年05月27日 09:08:58   作者:人生如初見_張默  
這篇文章主要介紹了PHP code 驗證碼生成類定義和簡單使用,結(jié)合實例形式分析了PHP code 驗證碼生成類的基本功能定義、簡單使用方法及操作注意事項,需要的朋友可以參考下

本文實例講述了PHP code 驗證碼生成類定義和簡單使用。分享給大家供大家參考,具體如下:

code.php

<?php
namespace code;
/**
 * Class Code
 */
class Code
{
  protected $number;//驗證碼內(nèi)字符個數(shù)
  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);
  }
 
  /**
   * 外部調(diào)用code時觸發(fā)
   * @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;
  }
 
  /**
   * 數(shù)字驗證碼
   * @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);
  }
 
  /**
   * 字符和數(shù)字混合驗證碼
   * @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();//創(chuàng)建畫布
    $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),
];

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • 詳細解讀PHP中接口的應(yīng)用

    詳細解讀PHP中接口的應(yīng)用

    這篇文章主要介紹了PHP中接口的應(yīng)用,是PHP入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • php給圖片加文字水印

    php給圖片加文字水印

    本文給大家分享的是使用php實現(xiàn)的給圖片加水印的方法,十分的細致全面,有需要的小伙伴可以參考下。
    2015-07-07
  • php數(shù)組合并與拆分實例分析

    php數(shù)組合并與拆分實例分析

    這篇文章主要介紹了php數(shù)組合并與拆分方法,實例分析了php中array_merge、array_merge_recursive、array_splice等方法操作數(shù)組的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • php數(shù)組的一些常見操作匯總

    php數(shù)組的一些常見操作匯總

    數(shù)組是最基本的數(shù)據(jù)結(jié)構(gòu),關(guān)于數(shù)組的操作是程序員最經(jīng)常用到的。這里將一些常用的操作寫成函數(shù)。
    2011-07-07
  • 一文搞懂PHP中的抽象類和接口

    一文搞懂PHP中的抽象類和接口

    這篇文章主要介紹了PHP中抽象類和接口的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 解析PHP提交后跳轉(zhuǎn)

    解析PHP提交后跳轉(zhuǎn)

    本篇文章是對PHP提交后跳轉(zhuǎn)進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP入門教程之日期與時間操作技巧總結(jié)(格式化,驗證,獲取,轉(zhuǎn)換,計算等)

    PHP入門教程之日期與時間操作技巧總結(jié)(格式化,驗證,獲取,轉(zhuǎn)換,計算等)

    這篇文章主要介紹了PHP入門教程之日期與時間操作技巧,結(jié)合實例形式總結(jié)分析了php針對日期與時間的驗證、格式化、獲取、轉(zhuǎn)換、計算等相關(guān)操作技巧,需要的朋友可以參考下
    2016-09-09
  • php數(shù)組(array)輸出的三種形式詳解

    php數(shù)組(array)輸出的三種形式詳解

    本篇文章是對php數(shù)組(array)輸出的三種形式進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 超強多功能php綠色集成環(huán)境詳解

    超強多功能php綠色集成環(huán)境詳解

    本文主要介紹了超強多功能php綠色集成環(huán)境,文中所使用的是PHPWAMP這款綠色的集成環(huán)境,集成VC運行庫。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • 基于php+redis實現(xiàn)布隆過濾器

    基于php+redis實現(xiàn)布隆過濾器

    布隆過濾器(Bloom filter)是一種用于快速判斷一個元素是否存在于集合中的數(shù)據(jù)結(jié)構(gòu),它可以有效地檢索數(shù)據(jù),而不需要存儲實際的元素本身,本文給大家介紹了如何基于php+redis實現(xiàn)布隆過濾器,感興趣的朋友可以參考下
    2023-12-12

最新評論