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

一個實用的php驗證碼類

 更新時間:2017年07月06日 11:29:44   作者:東東東蔚  
這篇文章主要為大家詳細介紹了一個實用的php驗證碼類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

萬能php驗證碼類,供大家參考,具體內(nèi)容如下

code.php是驗證碼類,類的名稱最好和文件名的名稱一樣,這樣有利于我們的查看。

code.php

<?php
header('Content-type:text/html;charset=utf8');
class Code{
  // 驗證碼個數(shù)$number
  protected $number;
  // 驗證碼類型$codeType
  protected $codeType;
  // 驗證碼圖像寬度$width
  protected $width;
  // 驗證碼$height
  protected $height;
  // 驗證碼字符串$code
  protected $code;
  // 圖像資源$image
  protected $image;
  
  public function __construct($number=4,$codeType=0,$height=50,$width=100){
    //初始化自己的成員屬性
    $this->number=$number;
    $this->codeType=$codeType;
    $this->width = $width;
    $this->height= $height;
    
    //生成驗證碼函數(shù)
    $this->code = $this ->createCode();
    
  }
  public function __get($name){
    if ($name == 'code'){
      return $this->code;
    }
    return false;
  }
  /*獲取驗證碼*/
  public function getCode() {
    return $this->code;
  }
  /*圖像資源銷毀*/
  public function __destruct(){
    imagedestroy($this->image);
  }
  protected function createCode(){
    //通過你的驗證碼類型生成驗證碼
    switch ($this->codeType){
      case 0: //純數(shù)字
        $code = $this->getNumberCode();
        break;
      case 1: //純字母的
        $code = $this->getCharCode();
        break;
      case 2: //數(shù)字和字母混合
        $code = $this->getNumCharCode();
        break;
      default:
        die('不支持此類驗證碼類型');
    }
    return $code;
  }
  protected function getNumberCode(){
    $str = join('', range(0, 9));
    return substr(str_shuffle($str),0, $this->number);
  }
  protected function getCharCode(){
    $str = join('', range('a', 'z'));
    $str = $str.strtoupper($str);
    return substr(str_shuffle($str),0,$this->number);
  }
  protected function getNumCharCode(){
    $numstr = join('',range(0, 9));
    $str =join('', range('a', 'z'));
    $str =$numstr.$str.strtoupper($str);
    return substr(str_shuffle($str), 0,$this->number);
  }
  protected function createImage(){
    $this->image = imagecreatetruecolor($this->width, 
        $this->height);
  }
  protected function fillBack(){
    imagefill($this->image, 0, 0, $this->lightColor());
  }
  /*淺色*/
  protected function lightColor(){
    return imagecolorallocate($this->image, mt_rand(133,255), mt_rand(133,255), mt_rand(133,255));
  }
  /*深色*/
  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-10);
      $y = mt_rand(0, $this->height -15);
      imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
    }
  }
  protected function drawLine(){
    for ($i=0;$i<5;$i++) {
      imageline($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor());
    }
  }
  protected function drawDisturb(){
    for ($i=0;$i<150;$i++){
      $x=mt_rand(0, $this->width);
      $y=mt_rand(0, $this->height);
      imagesetpixel($this->image, $x, $y, $this->lightColor());
    }
  }
  protected function show(){
    header('Content-Type:image/png');
    imagepng($this->image);
  }
  public function outImage(){
//     創(chuàng)建畫布
    $this->createImage();
//     填充背景色
    $this->fillBack();
//     將驗證碼字符串花到畫布上
    $this->drawChar();
//     添加干擾元素
    $this->drawDisturb();
//     添加線條
    $this->drawLine();
//     輸出并顯示
    $this->show();
  }
}

test.php是new一個新的驗證碼,并把它保存到session中,為我們驗證碼的驗證起到保存和存儲的作用。

test.php

<?php
//開啟session
session_start();
require_once 'code.php';

$code= new Code(4,1,50,100);
$_SESSION['code']= $code->getCode();
$code->outImage();

login.php就是最后的驗證。

login.php

<?php 
    //開啟Session 
    session_start(); 
    //判斷是否提交 
    if(isset($_POST['dosubmit'])){ 
      //獲取session中的驗證碼并轉(zhuǎn)為小寫 
      $sessionCode=strtolower($_SESSION['code']); 
      //獲取輸入的驗證碼 
      $code=strtolower($_POST['code']); 
      //判斷是否相等 
      if($sessionCode==$code){ 
        echo "<script type='text/javascript'>alert('驗證碼正確!');</script>"; 
      }else{ 
        echo "<script type='text/javascript'>alert('驗證碼錯誤!');</script>"; 
      } 
    } 
  ?> 
  <!DOCTYPE html> 
  <html> 
    <head> 
      <title></title> 
      <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 
      <style type="text/css"> 
        *{margin:0px;padding:0px;} 
        ul{ 
          width:400px; 
          list-style:none; 
          margin:50px auto; 
        } 
         
        li{ 
          padding:12px; 
          position:relative; 
        } 
         
        label{ 
          width:80px; 
          display:inline-block; 
          float:left; 
          line-height:30px; 
        } 
         
        input[type='text'],input[type='password']{ 
          height:30px; 
        } 
         
        img{ 
          margin-left:10px; 
        } 
         
        input[type="submit"]{ 
          margin-left:80px; 
          padding:5px 10px; 
        } 
      </style> 
    </head> 
    <body> 
      <form action="login.php" method="post"> 
        <ul> 
          <li> 
            <label>用戶名:</label> 
            <input type="text" name="username"/> 
          </li> 
          <li> 
            <label>密碼:</label> 
            <input type="password" name="password"/> 
          </li> 
          <li> 
            <label>驗證碼:</label> 
            <input type="text" name="code" size="4" style="float:left"/> 
            <img src="test.php" onclick="this.src='test.php?Math.random()'"/> 
          </li> 
          <li> 
            <input type="submit" value="登錄" name="dosubmit"/> 
          </li> 
        </ul> 
      </form> 
    </body> 
  </html>

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

相關(guān)文章

  • ThinkPHP5 框架引入 Go AOP,PHP AOP編程項目詳解

    ThinkPHP5 框架引入 Go AOP,PHP AOP編程項目詳解

    這篇文章主要介紹了ThinkPHP5 框架引入 Go AOP,PHP AOP編程,結(jié)合具體項目項目分析了ThinkPHP5 引入 Go AOP,PHP AOP編程相關(guān)概念、原理、操作技巧與注意事項,需要的朋友可以參考下
    2020-05-05
  • PHP isset empty函數(shù)相關(guān)面試題及解析

    PHP isset empty函數(shù)相關(guān)面試題及解析

    這篇文章主要介紹了PHP isset empty函數(shù)相關(guān)面試題及解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • Yii框架日志記錄Logging操作示例

    Yii框架日志記錄Logging操作示例

    這篇文章主要介紹了Yii框架日志記錄Logging操作,結(jié)合實例形式分析了Yii框架日志記錄Logging模塊相關(guān)配置與簡單使用技巧,需要的朋友可以參考下
    2018-07-07
  • phpcms中的評論樣式修改方法

    phpcms中的評論樣式修改方法

    phpcms中自帶的評論插件很好用,但是樣式不太好看,今天小編給大家分享兩種關(guān)于phpcms中的評論樣式修改方法,一起看看吧
    2016-10-10
  • YII框架行為behaviors用法示例

    YII框架行為behaviors用法示例

    這篇文章主要介紹了YII框架行為behaviors用法,結(jié)合實例形式分析了Yii框架行為behaviors的添加與使用簡單操作技巧,需要的朋友可以參考下
    2019-04-04
  • 一個模仿oso的php論壇程序源碼(之三)

    一個模仿oso的php論壇程序源碼(之三)

    一個模仿oso的php論壇程序源碼(之三)...
    2007-03-03
  • PHP環(huán)境中Memcache的安裝和使用

    PHP環(huán)境中Memcache的安裝和使用

    本文給大家介紹php環(huán)境中memcache的安裝和使用,它可以應(yīng)用任意多個連接,使用非阻塞的網(wǎng)絡(luò)IO。由于它的工作機制是在內(nèi)存中開辟一塊空間,然后建立一個HashTable,Memcached自管理這些HashTable,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • Ubuntu中啟用php的mail()函數(shù)并解決發(fā)送郵件速度慢問題

    Ubuntu中啟用php的mail()函數(shù)并解決發(fā)送郵件速度慢問題

    本文主要給大家介紹的是在Ubuntu下安裝sendmail的方法,以及啟用sendmail之后,php發(fā)送郵件緩慢的原因及解決方法,有需要的小伙伴可以參考下。
    2015-03-03
  • thinkphp5.1 文件引入路徑問題及注意事項

    thinkphp5.1 文件引入路徑問題及注意事項

    這篇文章主要介紹了thinkphp5.1 文件引入路徑問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • 詳解PHP實現(xiàn)支付寶小程序用戶授權(quán)的工具類

    詳解PHP實現(xiàn)支付寶小程序用戶授權(quán)的工具類

    這篇文章主要介紹了詳解PHP實現(xiàn)支付寶小程序用戶授權(quán)的工具類,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12

最新評論

绥中县| 栾城县| 乐亭县| 长子县| 庆安县| 同江市| 镇远县| 盐亭县| 焉耆| 沙坪坝区| 西丰县| 新乡县| 金昌市| 沂源县| 开江县| 沈阳市| 大悟县| 谷城县| 德江县| 汽车| 泰和县| 湄潭县| 紫金县| 西林县| 永康市| 务川| 泊头市| 瑞昌市| 武乡县| 泗洪县| 香河县| 丹棱县| 慈利县| 高碑店市| 灵寿县| 年辖:市辖区| 田阳县| 台北县| 师宗县| 巴里| 金湖县|