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

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

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

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

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

code.php

<?php
header('Content-type:text/html;charset=utf8');
class Code{
  // 驗(yàn)證碼個(gè)數(shù)$number
  protected $number;
  // 驗(yàn)證碼類(lèi)型$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;
  }
  /*圖像資源銷(xiāo)毀*/
  public function __destruct(){
    imagedestroy($this->image);
  }
  protected function createCode(){
    //通過(guò)你的驗(yàn)證碼類(lèi)型生成驗(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('不支持此類(lèi)驗(yàn)證碼類(lèi)型');
    }
    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)建畫(huà)布
    $this->createImage();
//     填充背景色
    $this->fillBack();
//     將驗(yàn)證碼字符串花到畫(huà)布上
    $this->drawChar();
//     添加干擾元素
    $this->drawDisturb();
//     添加線(xiàn)條
    $this->drawLine();
//     輸出并顯示
    $this->show();
  }
}

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

test.php

<?php
//開(kāi)啟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 
    //開(kāi)啟Session 
    session_start(); 
    //判斷是否提交 
    if(isset($_POST['dosubmit'])){ 
      //獲取session中的驗(yàn)證碼并轉(zhuǎn)為小寫(xiě) 
      $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)證碼錯(cuò)誤!');</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>用戶(hù)名:</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>

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

相關(guān)文章

最新評(píng)論