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

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

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

本文實例講述了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時觸發(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;
  }
 
  /**
   * 數字驗證碼
   * @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();//創(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),
];

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

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

相關文章

  • PHP實現懶加載的方法

    PHP實現懶加載的方法

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

    PHP數組操作——獲取數組最后一個值的方法

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

    php經典算法集錦

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

    PHP 時間轉換Unix時間戳代碼

    PHP 時間轉換Unix 時間戳實現代碼。
    2010-01-01
  • 詳解PHP設計模式之依賴注入模式

    詳解PHP設計模式之依賴注入模式

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

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

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

    PHP session反序列化漏洞超詳細講解

    這篇文章主要介紹了PHP?session反序列化漏洞,php?session反序列化漏洞存在的原因是當序列化session和讀取反序列化字符時采用的序列化選擇器不一樣時,處理的方法不一樣
    2023-02-02
  • php自動注冊登錄驗證機制實現代碼

    php自動注冊登錄驗證機制實現代碼

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

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

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

    php實現圖片局部打馬賽克的方法

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

最新評論

夏河县| 乐亭县| 西城区| 沧源| 泸州市| 本溪市| 伽师县| 古交市| 美姑县| 西平县| 中阳县| 南岸区| 志丹县| 彩票| 石楼县| 上栗县| 大关县| 溧阳市| 富宁县| 台南县| 临沭县| 灌阳县| 迁西县| 伊通| 义乌市| 凤山县| 澎湖县| 蓝田县| 景谷| 神木县| 酒泉市| 石林| 定边县| 河池市| 涡阳县| 三都| 林甸县| 曲麻莱县| 息烽县| 迭部县| 泰兴市|