欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)證碼長(zhǎng)度,常見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)證碼長(zhǎng)度) * (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í)候取比較淡的顏色值,目的是盡可能的不影響人眼識(shí)別驗(yàn)證碼,又能干擾自動(dòng)識(shí)別驗(yàn)證碼機(jī)制。

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

//對(duì)外生成
  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);
  }

  //對(duì)外生成
  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.測(cè)試

測(cè)試代碼:

<?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ú)立測(cè)試過程,沒發(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)證碼這塊問題。雖然此方法簡(jiǎn)單,但這可能會(huì)引起其他緩沖數(shù)據(jù)問題,因?yàn)閐b_clean()功能就是丟棄輸出緩沖區(qū)中的內(nèi)容。

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

相關(guān)文章

  • PHP中的類型提示(type hinting)功能介紹

    PHP中的類型提示(type hinting)功能介紹

    這篇文章主要介紹了PHP中的類型提示(type hinting)功能介紹,本文講解了類型提示的作用和使用方法以及使用示例,需要的朋友可以參考下
    2015-07-07
  • 使用PHPCMS搭建wap手機(jī)網(wǎng)站

    使用PHPCMS搭建wap手機(jī)網(wǎng)站

    PHPCMS搭建PC端網(wǎng)站比較方便,但是在wap手機(jī)端方面卻不怎么實(shí)用,而且自帶的手機(jī)建站感覺不是很好,而且模版不好控制,現(xiàn)在對(duì)其進(jìn)行修改,手機(jī)建站個(gè)人感覺比較方便。本文給大家介紹使用PHPCMS搭建wap手機(jī)網(wǎng)站,需要的朋友可以參考下
    2015-09-09
  • PHP實(shí)現(xiàn)生成唯一編號(hào)(36進(jìn)制的不重復(fù)編號(hào))

    PHP實(shí)現(xiàn)生成唯一編號(hào)(36進(jìn)制的不重復(fù)編號(hào))

    這篇文章主要介紹了PHP實(shí)現(xiàn)生成唯一編號(hào),文中使用10進(jìn)制轉(zhuǎn)換36進(jìn)制得到不重復(fù)的6000多萬個(gè)唯一編號(hào),編號(hào)位數(shù)為10位,需要的朋友可以參考下
    2014-07-07
  • php的lavarel框架中join和orWhere的用法

    php的lavarel框架中join和orWhere的用法

    這篇文章主要介紹了php的lavarel框架中join和orWhere的用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • php下載文件,添加響應(yīng)頭的簡(jiǎn)單實(shí)例

    php下載文件,添加響應(yīng)頭的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)硪黄猵hp下載文件,添加響應(yīng)頭的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09
  • Yii2框架BootStrap樣式的深入理解

    Yii2框架BootStrap樣式的深入理解

    Yii2框架默認(rèn)采用了bootstrap作為CSS風(fēng)格,各種視圖類組件都如此。這篇文章主要介紹了Yii2框架BootStrap樣式理解的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • PHP實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

    PHP實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • PHP命令空間namespace及use的用法小結(jié)

    PHP命令空間namespace及use的用法小結(jié)

    命名空間一個(gè)最明確的目的就是解決重名問題,PHP中不允許兩個(gè)函數(shù)或者類出現(xiàn)相同的名字,否則會(huì)產(chǎn)生一個(gè)致命的錯(cuò)誤。這篇文章主要介紹了PHP命令空間namespace及use的用法實(shí)踐總結(jié),需要的朋友可以參考下
    2017-11-11
  • PHP mkdir()無寫權(quán)限的問題解決方法

    PHP mkdir()無寫權(quán)限的問題解決方法

    這篇文章主要介紹了PHP mkdir()無寫權(quán)限的問題解決方法,對(duì)umask做了詳細(xì)解釋以及mkdir()后沒寫權(quán)限的解決方法,需要的朋友可以參考下
    2014-06-06
  • 在PHP8中向數(shù)組添加元素的常用方法

    在PHP8中向數(shù)組添加元素的常用方法

    這篇文章主要介紹了在PHP8中向數(shù)組添加元素的常用方法,在這里主要講解幾個(gè)常用的方法:使用方括號(hào)[]添加元素、使用array_unshift()函數(shù),向數(shù)組的頭部添加元素、使用array_push()函數(shù),向數(shù)組的尾部添加元素、使用array_splice()函數(shù)添加元素,需要的朋友可以參考下
    2023-09-09

最新評(píng)論