国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) 後端開(kāi)發(fā) php教程 關(guān)於php click captcha 驗(yàn)證碼類別的介紹

關(guān)於php click captcha 驗(yàn)證碼類別的介紹

Jun 11, 2018 am 10:21 AM
captcha php

需求:

現(xiàn)在常用的表單驗(yàn)證碼大部分都是要使用者輸入為主,但這樣對(duì)手機(jī)使用者會(huì)不方便。

如果手機(jī)用戶訪問(wèn),可以不用輸入,而是click某一位置便可確認(rèn)驗(yàn)證碼,這樣就會(huì)方便很多。

原則:

#1.使用PHP?imagecreate建立PNG圖象,在圖中畫(huà)N個(gè)圓弧,其中一個(gè)是完整的圓(驗(yàn)證用),將圓心座標(biāo)及半徑記錄入session。

2.在瀏覽器,當(dāng)使用者在驗(yàn)證碼圖片上點(diǎn)擊時(shí),記錄點(diǎn)擊的位置。

3.將使用者點(diǎn)選的座標(biāo)與session記錄的圓心座標(biāo)、半徑比較,判斷是否在圓中,如是則驗(yàn)證通過(guò)。


ClickCaptcha.class.php

<?php
/** Click Captcha 驗(yàn)證碼類
*   Date:   2013-05-04
*   Author: fdipzone
*   Ver:    1.0
*/

class ClickCaptcha { // class start

    public $sess_name = &#39;m_captcha&#39;;
    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=&#39;&#39;){
        if(session_id() == &#39;&#39;){
            session_start();
        }

        if($sess_name!=&#39;&#39;){
            $this->sess_name = $sess_name; // 設(shè)置session name
        }
    }


    /** 創(chuàng)建驗(yàn)證碼 */
    public function create(){

        // 創(chuàng)建圖象
        $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);            
        }

        // 驗(yàn)證用圖象,完整的圓
        $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);        

        // 記錄圓心坐標(biāo)及半徑
        $this->captcha_session($this->sess_name, array($x, $y, $r));

        // 生成圖象
        Header("Content-type: image/PNG");
        ImagePNG($this->_img_res);
        ImageDestroy($this->_img_res);

        exit();
    }


    /** 檢查驗(yàn)證碼
    * @param String $captcha  驗(yàn)證碼
    * @param int    $flag     驗(yàn)證成功后 0:不清除session 1:清除session
    * @return boolean
    */
    public function check($captcha, $flag=1){
        if(trim($captcha)==&#39;&#39;){
            return false;
        }
        
        if(!is_array($this->captcha_session($this->sess_name))){
            return false;
        }

        list($px, $py) = explode(&#39;,&#39;, $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,&#39;&#39;);
                }
                return true;
            }
        }
        return false;
    }


    /** 判斷點(diǎn)是否在圓中
    * @param int $px  點(diǎn)x
    * @param int $py  點(diǎn)y
    * @param int $cx  圓心x
    * @param int $cy  圓心y
    * @param int $cr  圓半徑
    * sqrt(x^2+y^2)<r
    */
    private function pointInArea($px, $py, $cx, $cy, $cr){
        $x = $cx-$px;
        $y = $cy-$py;
        return round(sqrt($x*$x + $y*$y))<$cr;
    }


    /** 驗(yàn)證碼session處理方法
    * @param   String   $name    captcha session name
    * @param   String   $value
    * @return  String
    */
    private function captcha_session($name,$value=null){
        if(isset($value)){
            if($value!==&#39;&#39;){
                $_SESSION[$name] = $value;
            }else{
                unset($_SESSION[$name]);
            }
        }else{
            return isset($_SESSION[$name])? $_SESSION[$name] : &#39;&#39;;
        }
    }

} // class end

?>

#demo.php

<?php
session_start();
require(&#39;ClickCaptcha.class.php&#39;);

if(isset($_GET[&#39;get_captcha&#39;])){ // get captcha
    $obj = new ClickCaptcha();
    $obj->create();
    exit();
}

if(isset($_POST[&#39;send&#39;]) && $_POST[&#39;send&#39;]==&#39;true&#39;){ // submit
    $name = isset($_POST[&#39;name&#39;])? trim($_POST[&#39;name&#39;]) : &#39;&#39;;
    $captcha = isset($_POST[&#39;captcha&#39;])? trim($_POST[&#39;captcha&#39;]) : &#39;&#39;;

    $obj = new ClickCaptcha();

    if($obj->check($captcha)){
        echo &#39;your name is:&#39;.$name;
    }else{
        echo &#39;captcha not match&#39;;
    }
    echo &#39; <a href="demo.php">back</a>&#39;;

}else{ // html
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title> Click Captcha Demo </title>
  <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
  <script type="text/javascript">
    $(function(){
        $(&#39;#captcha_img&#39;).click(function(e){
            var x = e.pageX - $(this).offset().left;
            var y = e.pageY - $(this).offset().top;
            $(&#39;#captcha&#39;).val(x+&#39;,&#39;+y);
        })

        $(&#39;#btn&#39;).click(function(e){
            if($.trim($(&#39;#name&#39;).val())==&#39;&#39;){
                alert(&#39;Please input name!&#39;);
                return false;
            }

            if($.trim($(&#39;#captcha&#39;).val())==&#39;&#39;){
                alert(&#39;Please click captcha!&#39;);
                return false;
            }
            $(&#39;#form1&#39;)[0].submit();
        })
    })
  </script>
 </head>

 <body>
    <form name="form1" id="form1" method="post" action="demo.php" onsubmit="return false">
    <p>name:<input type="text" name="name" id="name"></p>
    <p>Captcha:Please click full circle<br><img id="captcha_img" src="demo.php?get_captcha=1&t=<?=time() ?>" style="cursor:pointer"></p>
    <p><input type="submit" id="btn" value="submit"></p>
    <input type="hidden" name="send" value="true">
    <input type="hidden" name="captcha" id="captcha">
    </form>
 </body>
</html>
<?php } ?>

這篇文章講解了關(guān)於php click captcha 驗(yàn)證碼類的介紹,更多相關(guān)內(nèi)容請(qǐng)關(guān)注php中文網(wǎng)。

相關(guān)推薦:

?關(guān)於HTML5 history API 的介紹

關(guān)於冒泡,二分法插入,快速排序演算法的介紹

講解php 支援?dāng)帱c(diǎn)續(xù)傳的檔案下載類別的相關(guān)內(nèi)容

以上是關(guān)於php click captcha 驗(yàn)證碼類別的介紹的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

熱門(mén)話題

如何在PHP中獲取當(dāng)前的會(huì)話ID? 如何在PHP中獲取當(dāng)前的會(huì)話ID? Jul 13, 2025 am 03:02 AM

在PHP中獲取當(dāng)前會(huì)話ID的方法是使用session_id()函數(shù),但必須先調(diào)用session_start()才能成功獲取。 1.調(diào)用session_start()啟動(dòng)會(huì)話;2.使用session_id()讀取會(huì)話ID,輸出類似abc123def456ghi789的字符串;3.若返回為空,檢查是否遺漏session_start()、用戶是否首次訪問(wèn)或會(huì)話是否被銷毀;4.會(huì)話ID可用於日誌記錄、安全驗(yàn)證和跨請(qǐng)求通信,但需注意安全性。確保正確開(kāi)啟會(huì)話後即可順利獲取ID。

php從字符串獲取子字符串 php從字符串獲取子字符串 Jul 13, 2025 am 02:59 AM

要從PHP字符串中提取子字符串,可使用substr()函數(shù),其語(yǔ)法為substr(string$string,int$start,?int$length=null),若未指定長(zhǎng)度則截取至末尾;處理多字節(jié)字符如中文時(shí)應(yīng)使用mb_substr()函數(shù)以避免亂碼;若需根據(jù)特定分隔符截取字符串,可使用explode()或結(jié)合strpos()與substr()實(shí)現(xiàn),例如提取文件名擴(kuò)展名或域名。

您如何執(zhí)行PHP代碼的單元測(cè)試? 您如何執(zhí)行PHP代碼的單元測(cè)試? Jul 13, 2025 am 02:54 AM

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

如何將字符串分為PHP中的數(shù)組 如何將字符串分為PHP中的數(shù)組 Jul 13, 2025 am 02:59 AM

在PHP中,最常用的方法是使用explode()函數(shù)將字符串拆分為數(shù)組。該函數(shù)通過(guò)指定的分隔符將字符串分割成多個(gè)部分並返回?cái)?shù)組,語(yǔ)法為explode(separator,string,limit),其中separator為分隔符,string為原字符串,limit為可選參數(shù)控制最大分割數(shù)量。例如$str="apple,banana,orange";$arr=explode(",",$str);結(jié)果為["apple","bana

JavaScript數(shù)據(jù)類型:原始與參考 JavaScript數(shù)據(jù)類型:原始與參考 Jul 13, 2025 am 02:43 AM

JavaScript的數(shù)據(jù)類型分為原始類型和引用類型。原始類型包括string、number、boolean、null、undefined和symbol,其值不可變且賦值時(shí)復(fù)制副本,因此互不影響;引用類型如對(duì)象、數(shù)組和函數(shù)存儲(chǔ)的是內(nèi)存地址,指向同一對(duì)象的變量會(huì)相互影響。判斷類型可用typeof和instanceof,但需注意typeofnull的歷史問(wèn)題。理解這兩類差異有助於編寫(xiě)更穩(wěn)定可靠的代碼。

在C中使用std :: Chrono 在C中使用std :: Chrono Jul 15, 2025 am 01:30 AM

std::chrono在C 中用於處理時(shí)間,包括獲取當(dāng)前時(shí)間、測(cè)量執(zhí)行時(shí)間、操作時(shí)間點(diǎn)與持續(xù)時(shí)間及格式化解析時(shí)間。 1.獲取當(dāng)前時(shí)間使用std::chrono::system_clock::now(),可轉(zhuǎn)換為可讀字符串但係統(tǒng)時(shí)鐘可能不單調(diào);2.測(cè)量執(zhí)行時(shí)間應(yīng)使用std::chrono::steady_clock以確保單調(diào)性,並通過(guò)duration_cast轉(zhuǎn)換為毫秒、秒等單位;3.時(shí)間點(diǎn)(time_point)和持續(xù)時(shí)間(duration)可相互操作,但需注意單位兼容性和時(shí)鐘紀(jì)元(epoch)

如何將會(huì)話變量傳遞給PHP中的另一頁(yè)? 如何將會(huì)話變量傳遞給PHP中的另一頁(yè)? Jul 13, 2025 am 02:39 AM

在PHP中,要將一個(gè)會(huì)話變量傳到另一個(gè)頁(yè)面,關(guān)鍵在於正確開(kāi)啟會(huì)話並使用相同的$_SESSION鍵名。 1.每個(gè)頁(yè)面使用session變量前必須調(diào)用session_start(),且放在腳本最前面;2.在第一個(gè)頁(yè)面設(shè)置session變量如$_SESSION['username']='JohnDoe';3.在另一頁(yè)面同樣調(diào)用session_start()後通過(guò)相同鍵名訪問(wèn)變量;4.確保每個(gè)頁(yè)面都調(diào)用session_start()、避免提前輸出內(nèi)容、檢查服務(wù)器上session存儲(chǔ)路徑可寫(xiě);5.使用ses

PHP如何處理環(huán)境變量? PHP如何處理環(huán)境變量? Jul 14, 2025 am 03:01 AM

toAccessenvironmentVariablesInphp,useGetenv()或$ _envsuperglobal.1.getEnv('var_name')retievesSpecificvariable.2。 $ _ en v ['var_name'] accessesvariablesifvariables_orderInphp.iniincludes“ e” .setVariablesViaCliWithvar = vualitephpscript.php,inapach

See all articles