一個簡單安全的PHP驗證碼類 附調用方法,php驗證碼附調用_PHP教程

來源:互聯網
上載者:User

一個簡單安全的PHP驗證碼類 附調用方法,php驗證碼附調用


一、驗證碼樣本

二、php驗證碼類,secoder.class.php

<?php/** * 安全驗證碼 *  * 安全的驗證碼要:驗證碼文字扭曲、旋轉,使用不同字型,添加幹擾碼 * * @author 流水孟春  * @link http://labs.yulans.cn/YL_Security_Secoder * @link http://wiki.yulans.cn/docs/yl/security/secoder */class YL_Security_Secoder { /** * 驗證碼的session的下標 *  * @var string */ //public static $seKey = 'sid.sek ey.ylans.cn'; public static $seKey = 'sid'; public static $expire = 3000;  // 驗證碼到期時間(s) /** * 驗證碼中使用的字元,01IO容易混淆,建議不用 * * @var string */ public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY'; public static $fontSize = 25;  // 驗證碼字型大小(px) public static $useCurve = true; // 是否畫混淆曲線 public static $useNoise = true; // 是否添加雜點  public static $imageH = 0;  // 驗證碼圖片寬 public static $imageL = 0;  // 驗證碼圖片長 public static $length = 4;  // 驗證碼位元 public static $bg = array(243, 251, 254); // 背景  protected static $_image = null;  // 驗證碼圖片執行個體 protected static $_color = null;  // 驗證碼字型顏色  /** * 輸出驗證碼並把驗證碼的值儲存的session中 * 驗證碼儲存到session的格式為: $_SESSION[self::$seKey] = array('code' => '驗證碼值', 'time' => '驗證碼建立時間'); */ public static function entry() { // 圖片寬(px) self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5;  // 圖片高(px) self::$imageH || self::$imageH = self::$fontSize * 2; // 建立一幅 self::$imageL x self::$imageH 的映像 self::$_image = imagecreate(self::$imageL, self::$imageH);  // 設定背景   imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]);  // 驗證碼字型隨機顏色 self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120)); // 驗證碼使用隨機字型  //$ttf = dirname(__FILE__) . '/ttfs/' . mt_rand(1, 20) . '.ttf'; 4 $ttf = dirname(__FILE__) . '/ttfs/4.ttf';   if (self::$useNoise) { // 繪雜點 self::_writeNoise(); }  if (self::$useCurve) { // 繪幹擾線 self::_writeCurve(); }  // 繪驗證碼 $code = array(); // 驗證碼 $codeNX = 0; // 驗證碼第N個字元的左邊距 for ($i = 0; $i 0) {   imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 這裡畫像素點比imagettftext和imagestring效能要好很多    $i--; } } }  $A = mt_rand(1, self::$imageH/2);     // 振幅  $f = mt_rand(-self::$imageH/4, self::$imageH/4); // X軸方向位移量 $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期 $w = (2* M_PI)/$T;  $b = $py - $A * sin($w*$px + $f) - self::$imageH/2; $px1 = $px2; $px2 = self::$imageL; for ($px=$px1; $px<=$px2; $px=$px+ 0.9) { if ($w!=0) { $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(ωx+φ) + b $i = (int) ((self::$fontSize - 8)/4); while ($i > 0) {   imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 這裡(while)迴圈畫像素點比imagettftext和imagestring用字型大小一次畫出(不用這while迴圈)效能要好很多   $i--; } } } }  /** * 畫雜點 * 往圖片上寫不同顏色的字母或數字 */ protected static function _writeNoise() { for($i = 0; $i < 10; $i++){ //雜點顏色  $noiseColor = imagecolorallocate(      self::$_image,       mt_rand(150,225),       mt_rand(150,225),       mt_rand(150,225)     ); for($j = 0; $j < 5; $j++) { // 繪雜點  imagestring(   self::$_image,   5,    mt_rand(-10, self::$imageL),    mt_rand(-10, self::$imageH),    self::$codeSet[mt_rand(0, 27)], // 雜點文本為隨機的字母或數字   $noiseColor  ); } } }  /** * 驗證驗證碼是否正確 * * @param string $code 使用者驗證碼 * @param bool 使用者驗證碼是否正確 */ public static function check($code) { isset($_SESSION) || session_start(); // 驗證碼不可為空 if(empty($code) || empty($_SESSION[self::$seKey])) { //echo $_SESSION[self::$seKey]['code'].'1'; return false;   } // session 到期 if(time() - $_SESSION[self::$seKey]['time'] > self::$expire) { unset($_SESSION[self::$seKey]); //echo $_SESSION[self::$seKey]['code'].'2'; return false; //return 0; }// if($code == $_SESSION[self::$seKey]['code']) { if(strtoupper($code) == $_SESSION[self::$seKey]['code']) { //不區分大小寫比較 //echo $_SESSION[self::$seKey]['code'].'3'; return true;  } //echo $_SESSION[self::$seKey]['code'].'4'; return false;  }}// useage/*YL_Security_Secoder::$useNoise = false; // 要更安全的話改成trueYL_Security_Secoder::$useCurve = true;YL_Security_Secoder::entry();*//*// 驗證驗證碼if (!YL_Security_Secoder::check(@$_POST['secode'])) { print 'error secode';}*/

三、調用方法

1.顯示驗證碼頁面code.php

<?php  session_start(); require 'secoder.class.php'; //先把類包含進來,實際路徑根據實際情況進行修改。  $vcode = new YL_Security_Secoder();  //執行個體化一個對象  $vcode->entry(); ?> 

2.檢查驗證碼是否正確

<?php  session_start(); require 'secoder.class.php'; //先把類包含進來,實際路徑根據實際情況進行修改。  $vcode = new YL_Security_Secoder();  //執行個體化一個對象  //$vcode->entry();  $code = $_GET['code'];  echo $vcode->check($code);   //$_SESSION['code'] = $vc->getCode();//驗證碼儲存到SESSION中?> 

3.驗證碼輸入框調用頁面

 單擊圖片重新擷取驗證碼

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

http://www.bkjia.com/PHPjc/1138985.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1138985.htmlTechArticle一個簡單安全的PHP驗證碼類 附調用方法,php驗證碼附調用 一、驗證碼樣本 二、php驗證碼類,secoder.class.php php/** * 安全驗證碼 * * 安全的驗...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.