最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP code 驗(yàn)證碼生成類定義和簡(jiǎn)單使用示例

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

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

code.php

<?php
namespace code;
/**
 * Class Code
 */
class Code
{
  protected $number;//驗(yàn)證碼內(nèi)字符個(gè)數(shù)
  protected $codeType;//驗(yàn)證碼樣式
  protected $width;//圖像寬
  protected $height;//圖像高
  protected $code;//驗(yàn)證碼
  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時(shí)觸發(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('樣式不對(duì)');
    }
    return $code;
  }
 
  /**
   * 數(shù)字驗(yàn)證碼
   * @return string
   */
  protected function getNum()
  {
    $str = join('', range(0,9));
    return substr(str_shuffle($str), 0, $this->number);
  }
 
  /**
   * 字符驗(yàn)證碼
   * @return string
   */
  protected function getChar()
  {
    $str = join('', range('a', 'z'));
    $str = $str . strtoupper($str);
    return substr(str_shuffle($str), 0, $this->number);
  }
 
  /**
   * 字符和數(shù)字混合驗(yàn)證碼
   * @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));
  }
 
  /**
   * 添加驗(yàn)證碼字符
   */
  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());
    }
  }
 
  /**
   * 添加干擾點(diǎn)
   */
  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();//添加驗(yàn)證字符
    $this->drawDisturb();//添加干擾點(diǎn)
    $this->drawArc();//添加干擾線
    $this->show();//輸出
  }
}

展示驗(yàn)證碼。。保存驗(yàn)證碼和過期時(shí)間

<?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è)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

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

相關(guān)文章

  • PHP實(shí)現(xiàn)懶加載的方法

    PHP實(shí)現(xiàn)懶加載的方法

    這篇文章主要介紹了PHP實(shí)現(xiàn)懶加載的方法,實(shí)例分析了php加載的原理與懶加載的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • PHP數(shù)組操作——獲取數(shù)組最后一個(gè)值的方法

    PHP數(shù)組操作——獲取數(shù)組最后一個(gè)值的方法

    這篇文章主要介紹了PHP數(shù)組操作——獲取數(shù)組最后一個(gè)值的方法,需要的朋友可以參考下
    2015-04-04
  • php經(jīng)典算法集錦

    php經(jīng)典算法集錦

    這篇文章主要介紹了php經(jīng)典算法,實(shí)例分析了漢諾塔、排序、查找、遞歸等算法技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • PHP 時(shí)間轉(zhuǎn)換Unix時(shí)間戳代碼

    PHP 時(shí)間轉(zhuǎn)換Unix時(shí)間戳代碼

    PHP 時(shí)間轉(zhuǎn)換Unix 時(shí)間戳實(shí)現(xiàn)代碼。
    2010-01-01
  • 詳解PHP設(shè)計(jì)模式之依賴注入模式

    詳解PHP設(shè)計(jì)模式之依賴注入模式

    依賴注入模式:依賴注入是控制反轉(zhuǎn)的一種實(shí)現(xiàn)方式。要實(shí)現(xiàn)控制反轉(zhuǎn),通常的解決方案是將創(chuàng)建被調(diào)用者實(shí)例的工作交由 IoC 容器來完成,然后在調(diào)用者中注入被調(diào)用者(通過構(gòu)造器 / 方法注入實(shí)現(xiàn)),這樣我們就實(shí)現(xiàn)了調(diào)用者與被調(diào)用者的解耦,該過程被稱為依賴注入。
    2021-05-05
  • php服務(wù)器的系統(tǒng)詳解

    php服務(wù)器的系統(tǒng)詳解

    在本篇文章里小編給大家整理的是關(guān)于php服務(wù)器用什么系統(tǒng)的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)參考下。
    2019-10-10
  • PHP session反序列化漏洞超詳細(xì)講解

    PHP session反序列化漏洞超詳細(xì)講解

    這篇文章主要介紹了PHP?session反序列化漏洞,php?session反序列化漏洞存在的原因是當(dāng)序列化session和讀取反序列化字符時(shí)采用的序列化選擇器不一樣時(shí),處理的方法不一樣
    2023-02-02
  • php自動(dòng)注冊(cè)登錄驗(yàn)證機(jī)制實(shí)現(xiàn)代碼

    php自動(dòng)注冊(cè)登錄驗(yàn)證機(jī)制實(shí)現(xiàn)代碼

    在phpwind站點(diǎn)后臺(tái)添加一個(gè)名為“廣告管家”(廣告管家為CNZZ的一款廣告投放的應(yīng)用)的應(yīng)用,整個(gè)“廣告管家”的應(yīng)用是通過iframe載入,載入的具體內(nèi)容根據(jù)不同站點(diǎn)顯示針對(duì)該站點(diǎn)的具體內(nèi)容
    2011-12-12
  • 控制PHP的輸出:緩存并壓縮動(dòng)態(tài)頁面

    控制PHP的輸出:緩存并壓縮動(dòng)態(tài)頁面

    PHP4中最令人滿意的事是——你可以讓PHP緩存所有由腳本生成的輸出,在你決定把它們送出之前,瀏覽器方是不會(huì)收到任何內(nèi)容的
    2013-06-06
  • php實(shí)現(xiàn)圖片局部打馬賽克的方法

    php實(shí)現(xiàn)圖片局部打馬賽克的方法

    這篇文章主要介紹了php實(shí)現(xiàn)圖片局部打馬賽克的方法,實(shí)例分析了php針對(duì)圖片操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-02-02

最新評(píng)論

仁怀市| 昂仁县| 乐业县| 鲁山县| 大邑县| 朔州市| 淳安县| 富蕴县| 西乌珠穆沁旗| 恩施市| 息烽县| 金溪县| 自贡市| 舞阳县| 磴口县| 云龙县| 平顶山市| 安吉县| 广安市| 同仁县| 建水县| 东光县| 林西县| 东丽区| 平阳县| 冀州市| 桐庐县| 宁乡县| 夏邑县| 普陀区| 新干县| 桐庐县| 阿拉善右旗| 桂东县| 德惠市| 吉木萨尔县| 黎平县| 分宜县| 越西县| 延川县| 芮城县|