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

PHP驗(yàn)證碼類ValidateCode解析

 更新時(shí)間:2017年01月07日 10:41:19   作者:ok_008  
這篇文章主要為大家詳細(xì)解析了PHP驗(yàn)證碼類ValidateCode,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

PHP解析驗(yàn)證碼類

1.開始

在網(wǎng)上看到使用PHP寫的ValidateCode生成驗(yàn)證碼類,感覺不錯(cuò),特拿來分析學(xué)習(xí)一下。

2.類圖

 

3.驗(yàn)證碼類部分代碼

3.1  定義變量

  //隨機(jī)因子
  private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789';
  private $code;
  private $codeLen = 4;

  private $width = 130;
  private $heigh = 50;
  private $img;//圖像

  private $font;//字體
  private $fontsize = 20;

$charset 是隨機(jī)因子,這里是去掉了幾個(gè)不容易區(qū)分的字符,如字母"i,l,o,q",數(shù)字"0,1"。有必要可以加入一些中文或其他字符或算式等。

$codeLen表示驗(yàn)證碼長度,常見4位。

3.2構(gòu)造函數(shù),設(shè)置驗(yàn)證碼字體,生成一個(gè)真彩色圖像img

public function __construct()
  {
    $this->font = ROOT_PATH.'/font/Chowderhead.ttf';
    $this->img = imagecreatetruecolor($this->width, $this->heigh);
  }

3.3從隨機(jī)因子中隨機(jī)抽取4個(gè)字符,作為$code驗(yàn)證碼.

//生成隨機(jī)碼
  private function createCode()
  {
    $_len = strlen($this->charset) - 1;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $this->code .= $this->charset[mt_rand(0, $_len)];
    }
  }

3.4生成驗(yàn)證碼背景色.

//生成背景
  private function createBg()
  {
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color);

  }

其中mt_rand(157, 255),目的是隨機(jī)取比較淺的顏色。

3.5在圖像上生成文字.

//生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codeLen;
    $_y = $this->heigh / 2;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
    }
  }

在圖像上生成驗(yàn)證碼文字,主要考慮文字在圖像上的位置和每一個(gè)文字顏色。

控制第n個(gè)文字的x軸位置 =  (圖像寬度 / 驗(yàn)證碼長度) * (n-1)  +  隨機(jī)的偏移數(shù);  其中n = {d1....n}

控制第n個(gè)文字的y軸位置 =  圖像高度 / 2 +  隨機(jī)的偏移數(shù);

mt_rand(0, 156) 隨機(jī)取文字顏色,0-156目的是取比較深的顏色。

mt_rand(-30, 30) 隨機(jī)的文字旋轉(zhuǎn)。

3.6在圖像上生成線條和雪花

//生成線條,雪花
  private function createLine()
  {
    for ($i = 0; $i < 15; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
    }
    for ($i = 0; $i < 150; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
      imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color);
    }
  }

畫線條的時(shí)候,取比較深的顏色值,而畫雪花的時(shí)候取比較淡的顏色值,目的是盡可能的不影響人眼識別驗(yàn)證碼,又能干擾自動(dòng)識別驗(yàn)證碼機(jī)制。

3.7對外生成驗(yàn)證碼圖像,供外部調(diào)用。

//對外生成
  public function doImg()
  {

    $this->createBg();   //1.創(chuàng)建驗(yàn)證碼背景
    $this->createCode();  //2.生成隨機(jī)碼
    $this->createLine();  //3.生成線條和雪花
    $this->createFont();  //4.生成文字
    $this->outPut();    //5.輸出驗(yàn)證碼圖像
  }

3.8完整代碼:

<?php

/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午1:20
 */
class ValidateCode
{
  //隨機(jī)因子
  private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789';
  private $code;
  private $codeLen = 4;

  private $width = 130;
  private $heigh = 50;
  private $img;//圖像

  private $font;//字體
  private $fontsize = 20;


  public function __construct()
  {
    $this->font = ROOT_PATH.'/font/Chowderhead.ttf';
    $this->img = imagecreatetruecolor($this->width, $this->heigh);
  }

  //生成隨機(jī)碼
  private function createCode()
  {
    $_len = strlen($this->charset) - 1;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $this->code .= $this->charset[mt_rand(0, $_len)];
    }
  }

  //生成背景
  private function createBg()
  {

    $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color);

  }

  //生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codeLen;
    $_y = $this->heigh / 2;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
    }
  }

  //生成線條,雪花
  private function createLine()
  {
    for ($i = 0; $i < 15; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
    }
    for ($i = 0; $i < 150; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
      imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color);
    }
  }

  //輸出圖像
  private function outPut()
  {
    header('Content-Type: image/png');
    imagepng($this->img);
    imagedestroy($this->img);
  }

  //對外生成
  public function doImg()
  {

    $this->createBg();   //1.創(chuàng)建驗(yàn)證碼背景
    $this->createCode();  //2.生成隨機(jī)碼
    $this->createLine();  //3.生成線條和雪花
    $this->createFont();  //4.生成文字
    $this->outPut();    //5.輸出驗(yàn)證碼圖像
  }

  //獲取驗(yàn)證碼
  public function getCode()
  {
    return strtolower($this->code);
  }

}

4.測試

測試代碼:

<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午1:20
 */

define('ROOT_PATH', dirname(__FILE__));
require_once ROOT_PATH.'/includes/ValidateCode.class.php';

$_vc=new ValidateCode();
echo $_vc->doImg();

生成驗(yàn)證碼:

5.應(yīng)用 

 <label>
<img src="../config/code.php" onclick="javascript:this.src='../config/code.php?tm='+Math.random();" />
</label>


上面onclick代碼是點(diǎn)擊驗(yàn)證碼圖片,能自動(dòng)刷新驗(yàn)證碼。

code.php:

<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午3:43
 */
require substr(dirname(__FILE__),0,-7).'/init.inc.php';

$_vc=new ValidateCode();
echo $_vc->doImg();
$_SESSION['ValidateCode']=$_vc->getCode();

有關(guān)應(yīng)用的完整代碼可以從https://git.oschina.net/andywww/myTest 的CMS1.0 文件里下載。

6.小結(jié)

在獨(dú)立測試過程,沒發(fā)現(xiàn)什么問題;但應(yīng)用到項(xiàng)目的時(shí)候,剛開始發(fā)現(xiàn)無法生成驗(yàn)證碼圖片,網(wǎng)上找了一下,有的說是在outPut()函數(shù)中,

在header('Content-Type: image/png'); 這行代碼前面增加了一行ob_clean()代碼,可以解決驗(yàn)證碼這塊問題。雖然此方法簡單,但這可能會(huì)引起其他緩沖數(shù)據(jù)問題,因?yàn)閐b_clean()功能就是丟棄輸出緩沖區(qū)中的內(nèi)容。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • openai createChatCompletion函數(shù)使用實(shí)例

    openai createChatCompletion函數(shù)使用實(shí)例

    這篇文章主要為大家介紹了openai createChatCompletion函數(shù)使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • CodeIgniter CLI模式簡介

    CodeIgniter CLI模式簡介

    這篇文章主要介紹了CodeIgniter CLI模式,需要的朋友可以參考下
    2014-06-06
  • php is_writable判斷文件是否可寫實(shí)例代碼

    php is_writable判斷文件是否可寫實(shí)例代碼

    這篇文章主要介紹了php is_writable判斷文件是否可寫實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • php檢測圖片木馬多進(jìn)制編程實(shí)踐

    php檢測圖片木馬多進(jìn)制編程實(shí)踐

    有制作圖片木馬的教程,并沒有找到檢測的程序,從制作原理來分析這種木馬程序,寫了以下這個(gè)上傳類與大家分享下,感性的朋友可以參考下
    2013-04-04
  • smarty模板引擎之分配數(shù)據(jù)類型

    smarty模板引擎之分配數(shù)據(jù)類型

    這篇文章主要介紹了smarty模板引擎之分配數(shù)據(jù)類型,實(shí)例分析了smarty模板引擎數(shù)據(jù)類型的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • Laravel使用swoole實(shí)現(xiàn)websocket主動(dòng)消息推送的方法介紹

    Laravel使用swoole實(shí)現(xiàn)websocket主動(dòng)消息推送的方法介紹

    這篇文章主要給大家介紹了關(guān)于Laravel使用swoole實(shí)現(xiàn)websocket主動(dòng)消息推送的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Laravel具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • PHP實(shí)現(xiàn)linux命令tail -f

    PHP實(shí)現(xiàn)linux命令tail -f

    tail 命令從指定點(diǎn)開始將文件寫到標(biāo)準(zhǔn)輸出.使用tail命令的-f選項(xiàng)可以方便的查閱正在改變的日志文件,tail -f filename會(huì)把filename里最尾部的內(nèi)容顯示在屏幕上,并且不但刷新,使你看到最新的文件內(nèi)容.接下來通過本文給大家介紹PHP實(shí)現(xiàn)linux命令tail -f,需要的朋友參考下
    2016-02-02
  • php教程之phpize使用方法

    php教程之phpize使用方法

    phpize是用來擴(kuò)展php擴(kuò)展模塊的,通過phpize可以建立php的模塊,下面介紹一個(gè)他的使用方法,需要的朋友可以參考下
    2014-02-02
  • Yii Framework框架獲取分類下面的所有子類方法

    Yii Framework框架獲取分類下面的所有子類方法

    Yii是一個(gè)基于組件、用于開發(fā)大型 Web 應(yīng)用的 高性能 PHP 框架。Yii 幾乎擁有了 所有的特性 ,包括 MVC、DAO/ActiveRecord、I18N/L10N、caching、基于 JQuery 的 AJAX 支持、用戶認(rèn)證和基于角色的訪問控制、腳手架、輸入驗(yàn)證、部件、事件、主題化以及 Web 服務(wù)等等。
    2014-06-06
  • ThinkPHP3.2.3框架實(shí)現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯(cuò)誤404頁面圖文詳解

    ThinkPHP3.2.3框架實(shí)現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯(cuò)誤404頁面圖文詳解

    這篇文章主要介紹了ThinkPHP3.2.3框架實(shí)現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯(cuò)誤404頁面,結(jié)合圖文形式詳細(xì)分析了thinkPHP3.2.3框架空模塊、空控制器、空操作,跳轉(zhuǎn)到錯(cuò)誤404頁面具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-04-04

最新評論

台东县| 阿荣旗| 新昌县| 定远县| 三都| 城口县| 新泰市| 于田县| 淮安市| 广元市| 阿克陶县| 望城县| 施秉县| 苗栗县| 新沂市| 渝北区| 泸定县| 姚安县| 略阳县| 汽车| 厦门市| 博罗县| 南宁市| 平江县| 工布江达县| 延长县| 河源市| 隆化县| 巴里| 石屏县| 伊川县| 原平市| 巧家县| 安吉县| 哈巴河县| 澄迈县| 扬州市| 理塘县| 滦南县| 阳东县| 历史|