php實現的click captcha點擊驗證碼類執行個體,clickcaptcha_PHP教程

來源:互聯網
上載者:User

php實現的click captcha點擊驗證碼類執行個體,clickcaptcha


本文執行個體講述了php實現的click captcha點擊驗證碼類及其用法,是非常實用的功能。分享給大家供大家參考之用。具體如下:

一、需求:

現在常用的表單驗證碼大部分都是要使用者輸入為主,但這樣對手機使用者會不方便。
如果手機使用者訪問,可以不用輸入,而是click某一位置便可確認驗證碼,這樣就會方便很多。

二、原理:

1.使用PHP imagecreate建立PNG圖象,在圖中畫N個圓弧,其中一個是完整的圓(驗證用),將圓心座標及半徑記錄入session。

2.在瀏覽器,當使用者在驗證碼圖片上點擊時,記錄點擊的位置。

3.將使用者點擊的座標與session記錄的圓心座標、半徑比較,判斷是否在圓中,如是則驗證通過。

程式運行效果如所示:

三、實現方法:

ClickCaptcha.class.php類檔案如下:

<?php /** Click Captcha 驗證碼類 *  Date:  2013-05-04 *  Author: fdipzone *  Ver:  1.0 */  class ClickCaptcha { // class start    public $sess_name = 'm_captcha';   public $width = 500;   public $height = 200;   public $icon = 5;   public $iconColor = array(255, 255, 0);   public $backgroundColor = array(0, 0, 0);   public $iconSize = 56;    private $_img_res = null;    public function __construct($sess_name=''){     if(session_id() == ''){       session_start();     }      if($sess_name!=''){       $this->sess_name = $sess_name; // 設定session name     }   }    /** 建立驗證碼 */   public function create(){      // 建立圖象     $this->_img_res = imagecreate($this->width, $this->height);          // 填充背景     ImageColorAllocate($this->_img_res, $this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]);      // 分配顏色     $col_ellipse = imagecolorallocate($this->_img_res, $this->iconColor[0], $this->iconColor[1], $this->iconColor[2]);      $minArea = $this->iconSize/2+3;      // 混淆用圖象,不完整的圓     for($i=0; $i<$this->icon; $i++){       $x = mt_rand($minArea, $this->width-$minArea);       $y = mt_rand($minArea, $this->height-$minArea);       $s = mt_rand(0, 360);       $e = $s + 330;       imagearc($this->_img_res, $x, $y, $this->iconSize, $this->iconSize, $s, $e, $col_ellipse);           }      // 驗證用圖象,完整的圓     $x = mt_rand($minArea, $this->width-$minArea);     $y = mt_rand($minArea, $this->height-$minArea);     $r = $this->iconSize/2;     imagearc($this->_img_res, $x, $y, $this->iconSize, $this->iconSize, 0, 360, $col_ellipse);          // 記錄圓心座標及半徑     $this->captcha_session($this->sess_name, array($x, $y, $r));      // 產生圖象     Header("Content-type: image/PNG");     ImagePNG($this->_img_res);     ImageDestroy($this->_img_res);      exit();   }    /** 檢查驗證碼   * @param String $captcha 驗證碼   * @param int  $flag   驗證成功後 0:不清除session 1:清除session   * @return boolean   */   public function check($captcha, $flag=1){     if(trim($captcha)==''){       return false;     }          if(!is_array($this->captcha_session($this->sess_name))){       return false;     }      list($px, $py) = explode(',', $captcha);     list($cx, $cy, $cr) = $this->captcha_session($this->sess_name);      if(isset($px) && is_numeric($px) && isset($py) && is_numeric($py) &&        isset($cx) && is_numeric($cx) && isset($cy) && is_numeric($cy) && isset($cr) && is_numeric($cr)){       if($this->pointInArea($px,$py,$cx,$cy,$cr)){         if($flag==1){           $this->captcha_session($this->sess_name,'');         }         return true;       }     }     return false;   }    /** 判斷點是否在圓中   * @param int $px 點x   * @param int $py 點y   * @param int $cx 圓心x   * @param int $cy 圓心y   * @param int $cr 圓半徑   * sqrt(x^2+y^2) 

demo.php樣本程式如下:

<?php session_start(); require('ClickCaptcha.class.php');  if(isset($_GET['get_captcha'])){ // get captcha   $obj = new ClickCaptcha();   $obj->create();   exit(); }  if(isset($_POST['send']) && $_POST['send']=='true'){ // submit   $name = isset($_POST['name'])? trim($_POST['name']) : '';   $captcha = isset($_POST['captcha'])? trim($_POST['captcha']) : '';    $obj = new ClickCaptcha();    if($obj->check($captcha)){     echo 'your name is:'.$name;   }else{     echo 'captcha not match';   }   echo ' back';  }else{ // html ?>         Click Captcha Demo                 <?php } ?> 

本文完整源碼點擊此處本站下載。

希望本文所述對大家的PHP程式設計有所協助。


php註冊頁面驗證碼驗證代碼 代碼如下:

你要判斷啊,判斷你這個填寫的驗證碼是不是跟你之前驗證碼儲存的是否一致

if($_POST["captcha"]!=$_SESSION["captcha"])
{
echo "驗證碼錯誤";
}
 

Zend_Captcha產生的驗證碼,點擊換一個怎弄? - PHP架構開發

如果你的意思是 Ajax 那樣的效果的話,那麼可以用類似這樣的方式 :$captcha = new Zend_Captcha_Image(array(......));$id = $captcha->generate();echo \'\' . $captcha->render($view) . \'\';// .jsfunction changeCaptcha(){? ???......? ???document.getElementById(\'captcha\').innerHtml = ......}
 

http://www.bkjia.com/PHPjc/882898.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/882898.htmlTechArticlephp實現的click captcha點擊驗證碼類執行個體,clickcaptcha 本文執行個體講述了php實現的click captcha點擊驗證碼類及其用法,是非常實用的功能。分享給大...

  • 聯繫我們

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