久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

一個實用的php驗證碼類

這篇文章主要為大家詳細介紹了一個實用的php驗證碼類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

萬能php驗證碼類,供大家參考,具體內容如下

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

code.php

<?php
header('Content-type:text/html;charset=utf8');
class Code{
  // 驗證碼個數$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;
    
    //生成驗證碼函數
    $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: //純數字
        $code = $this->getNumberCode();
        break;
      case 1: //純字母的
        $code = $this->getCharCode();
        break;
      case 2: //數字和字母混合
        $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(){
//     創建畫布
    $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中的驗證碼并轉為小寫 
      $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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要介紹了PHP定義字符串的四種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
下面小編就為大家分享一篇php 替換文章中的圖片路徑,下載圖片到本地服務器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP給源代碼加密的幾種方法匯總(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇php打開本地exe程序,js打開本地exe應用程序,并傳遞相關參數方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP類的反射來實現依賴注入過程以及相關知識點分享,對此有興趣的朋友跟著小編學習下吧。
php遍歷一個文件夾內的所有文件和文件夾,并刪除所有文件夾和子文件夾下的所有文件的代碼,通過遞歸方式實現達到清空一個目錄的效果。本文給大家分享實例代碼,需要的朋友參考
主站蜘蛛池模板: 最新免费黄色网址 | 产真a观专区 | 久久久久久久久久久久91 | 成人免费观看视频 | 黄色综合 | 欧美a级成人淫片免费看 | 午夜视频导航 | 免费xxxx大片国产在线 | 久久高清免费视频 | 暖暖成人免费视频 | 中文字幕av一区二区三区 | 欧美日韩久| 天天影视网天天综合色在线播放 | 欧美一区二区三区在线免费观看 | 综合久久久 | 一级二级三级黄色 | 免费看黄视频网站 | 亚洲成人黄色 | 欧美一区二区三区四区五区无卡码 | 亚洲精品一区二区三区蜜桃久 | 欧美高清视频一区 | 婷婷久久综合 | 国产一级片在线观看视频 | www.日韩| 国产在线第一页 | 中国一级特黄毛片大片 | 国产精品视频一 | 成人亚洲精品 | 一区二区三区四区在线视频 | av第一页| 久久久夜色精品亚洲 | 欧美日韩三级在线观看 | 日韩成人在线播放 | 狠狠av | 国产99精品 | 亚洲视频在线播放 | 亚洲二区视频 | av免费网址 | 国产精品一区一区 | 国产精品一区二区久久久久 | 高清亚洲 |