亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一個實(shí)用的php驗(yàn)證碼類

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

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

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

code.php

<?php
header('Content-type:text/html;charset=utf8');
class Code{
  // 驗(yàn)證碼個數(shù)$number
  protected $number;
  // 驗(yàn)證碼類型$codeType
  protected $codeType;
  // 驗(yàn)證碼圖像寬度$width
  protected $width;
  // 驗(yàn)證碼$height
  protected $height;
  // 驗(yàn)證碼字符串$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;
    
    //生成驗(yàn)證碼函數(shù)
    $this->code = $this ->createCode();
    
  }
  public function __get($name){
    if ($name == 'code'){
      return $this->code;
    }
    return false;
  }
  /*獲取驗(yàn)證碼*/
  public function getCode() {
    return $this->code;
  }
  /*圖像資源銷毀*/
  public function __destruct(){
    imagedestroy($this->image);
  }
  protected function createCode(){
    //通過你的驗(yàn)證碼類型生成驗(yàn)證碼
    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('不支持此類驗(yàn)證碼類型');
    }
    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();
//     將驗(yàn)證碼字符串花到畫布上
    $this->drawChar();
//     添加干擾元素
    $this->drawDisturb();
//     添加線條
    $this->drawLine();
//     輸出并顯示
    $this->show();
  }
}

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

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就是最后的驗(yàn)證。

login.php

<?php 
    //開啟Session 
    session_start(); 
    //判斷是否提交 
    if(isset($_POST['dosubmit'])){ 
      //獲取session中的驗(yàn)證碼并轉(zhuǎn)為小寫 
      $sessionCode=strtolower($_SESSION['code']); 
      //獲取輸入的驗(yàn)證碼 
      $code=strtolower($_POST['code']); 
      //判斷是否相等 
      if($sessionCode==$code){ 
        echo "<script type='text/javascript'>alert('驗(yàn)證碼正確!');</script>"; 
      }else{ 
        echo "<script type='text/javascript'>alert('驗(yàn)證碼錯誤!');</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>驗(yàn)證碼:</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)文章

  • PHP 自定義錯誤處理函數(shù)的使用詳解

    PHP 自定義錯誤處理函數(shù)的使用詳解

    本篇文章是對PHP自定義錯誤處理函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • ThinkPHP中redirect用法分析

    ThinkPHP中redirect用法分析

    這篇文章主要介紹了ThinkPHP中redirect用法,實(shí)例分析了redirect重定向的各種常見操作技巧,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • Thinkphp關(guān)閉緩存的方法

    Thinkphp關(guān)閉緩存的方法

    這篇文章主要介紹了Thinkphp關(guān)閉緩存的方法,開發(fā)項(xiàng)目時(shí)經(jīng)常需要調(diào)戲,有緩存的話不太方便,因此總出了關(guān)閉緩存的方法,需要的朋友可以參考下
    2015-06-06
  • laravel 解決多庫下的DB::transaction()事務(wù)失效問題

    laravel 解決多庫下的DB::transaction()事務(wù)失效問題

    今天小編就為大家分享一篇laravel 解決多庫下的DB::transaction()事務(wù)失效問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • Yii框架數(shù)據(jù)庫查詢、增加、刪除操作示例

    Yii框架數(shù)據(jù)庫查詢、增加、刪除操作示例

    這篇文章主要介紹了Yii框架數(shù)據(jù)庫查詢、增加、刪除操作,結(jié)合實(shí)例形式總結(jié)分析了Yii框架數(shù)據(jù)庫查詢、增加、刪除相關(guān)模型與控制器使用技巧,需要的朋友可以參考下
    2019-10-10
  • PHP使用PDO連接ACCESS數(shù)據(jù)庫

    PHP使用PDO連接ACCESS數(shù)據(jù)庫

    本文給大家分享的是一個簡單的php使用pdo方式連接access數(shù)據(jù)庫的方法,有需要的小伙伴可以參考下。
    2015-03-03
  • Twig模板引擎用法入門教程

    Twig模板引擎用法入門教程

    這篇文章主要介紹了Twig模板引擎用法,分析了Twig模板引擎的基本功能、安裝與簡單使用方法,需要的朋友可以參考下
    2016-01-01
  • Yii實(shí)現(xiàn)簡單分頁的方法

    Yii實(shí)現(xiàn)簡單分頁的方法

    這篇文章主要介紹了Yii實(shí)現(xiàn)簡單分頁的方法,涉及Yii模型調(diào)用讀取數(shù)據(jù)及視圖操作相關(guān)技巧,需要的朋友可以參考下
    2016-04-04
  • destoon公司主頁模板風(fēng)格的添加方法

    destoon公司主頁模板風(fēng)格的添加方法

    這篇文章主要介紹了destoon公司主頁模板風(fēng)格的添加方法,需要的朋友可以參考下
    2014-06-06
  • laravel實(shí)現(xiàn)簡單用戶權(quán)限的示例代碼

    laravel實(shí)現(xiàn)簡單用戶權(quán)限的示例代碼

    這篇文章主要介紹了laravel實(shí)現(xiàn)簡單用戶權(quán)限的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評論