php驗證碼類執行個體分享

來源:互聯網
上載者:User
本文主要為大家介紹了一個實用的php驗證碼類,具有一定的參考價值,感興趣的小夥伴們可以參考一下,希望能協助到大家。

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

code.php


<?phpheader('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//開啟sessionsession_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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.