搜尋
首頁後端開發php教程詳細介紹PHP驗證碼實作的原理

拓展

我們需要開啟gd拓展,可以使用下面的程式碼來檢視是否開啟gd拓展。

<?phpecho "Hello World!!!!";echo phpinfo();?>

然後在瀏覽器上Ctrl+F查找gd選項即可驗證自己有沒有裝這個拓展,如果沒有的話,還需要自己全裝一下這個拓展。

背景圖

imagecreatetruecolor

預設產生黑色背景

<?php
// 使用gd的
imagecreatetruecolor();创建一张背景图
$image = imagecreatetruecolor(100,30);// 在显示这张图片的时候一定要先声明头信息
header(&#39;content-type:image/png&#39;);

imagepng($image);// 释放资源,销毁执行对象imagedestroy($image);

imagecolorallocate

建立一個填滿色,並用imagefill(image,x ,y,color)方法來附著。

<?php
// 使用gd的
imagecreatetruecolor();创建一张背景图
$image = imagecreatetruecolor(100,30);// 生成填充色
$bgcolor = imagecolorallocate($image,255,255,255);// 将填充色填充到背景图上
imagefill($image,0,0,$bgcolor);// 在显示这张图片的时候一定要先声明头信息
header(&#39;content-type:image/png&#39;);

imagepng($image);// 释放资源,销毁执行对象imagedestroy($image);

imagepng

在使用這個方法之前,一定要先設定頭訊息,否則不會正常的顯示圖片

imagedestory(image)

適時的釋放資源會減輕對伺服器請求的壓力。

簡易數字驗證碼

imagecolorallocate

#產生顏色訊息,方便待會的賦予處理。

$fontcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));

imagestring

把內容資訊寫到圖片的對應位置。

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

增加識別幹擾

增加點

// 生成一些干扰的点,这里是200个
for($i=0;$i<200;$i++){    
$pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
    imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);
}

增加線

// 生成一些干扰线 这里是5个
for($i=0;$i<5;$i++){    // 设置为浅色的线,防止喧宾夺主
    $linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
    imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);

}

數字字母混合驗證碼

<?php
// 使用gd的imagecreatetruecolor();创建一张背景图
$image = imagecreatetruecolor(100,40);// 生成填充色
$bgcolor = imagecolorallocate($image,255,255,255);// 将填充色填充到背景图上
imagefill($image,0,0,$bgcolor);//////// 生成随机4位字母以及数字混合的验证码
for($i=0;$i<4;$i++){    
$fontsize = rand(6,8);    
$fontcolor = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));    
// 为了避免用户难于辨认,去掉了某些有歧义的字母和数字
    $rawstr = &#39;abcdefghjkmnopqrstuvwxyz23456789ABCDEFGHJKLMNOPQRSTUVWXYZ&#39;;    
    $fontcontent = substr($rawstr,rand(0,strlen($rawstr)),1);    
    // 避免生成的图片重叠
    $x += 20;    $y = rand(10,20);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);   
}// 生成一些干扰的点,这里是200个for($i=0;$i<200;$i++){    
$pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
    imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);
}
// 生成一些干扰线 这里是4个for($i=0;$i<4;$i++){    
// 设置为浅色的线,防止喧宾夺主
    $linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255));
    imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);

}


header(&#39;content-type:image/png&#39;);

imagepng($image);// 释放资源,销毁执行对象imagedestroy($image);

使用驗證碼

開啟session的時機

注意: 開啟session一定要在開始的地方

## 驗證的原理

驗證的過程就是客戶端輸入的驗證碼和存在於session域中的驗證碼進行比較。即:

if(isset($_REQUEST[&#39;checkcode&#39;])){
        session_start();        
        if($_REQUEST[&#39;checkcode&#39;]==$_SESSION[&#39;checkcode&#39;]){            
        echo "<font color=&#39;green&#39;>Success!</font>"; 
        }else{            
        echo "<font color=&#39;red&#39;>Failed!</font>";    
        }        
        exit();
    }

優化驗證

但是簡單的這樣驗證有一點不好的地方,那就是字母的大小寫容易出錯。所以我們要做一下轉換,將使用者輸入的數值全部變成小寫的。

if(strtolower($_REQUEST[&#39;checkcode&#39;])==$_SESSION[&#39;checkcode&#39;]){···}

小案例

產生驗證碼

表單驗證

<?php
header("Content-Type:text/html;charset=utf8");        
if(isset($_REQUEST[&#39;checkcode&#39;])){
            session_start();            
            if(strtolower($_REQUEST[&#39;checkcode&#39;])==$_SESSION[&#39;checkcode&#39;]){                
            echo "<font color=&#39;green&#39;>Success!</font>"; 
            }else{                
            echo "<font color=&#39;red&#39;>Failed!</font>";    
            }            
            exit();
        }?>
        <!DOCTYPE html><html><head>
    <meta charset="utf-8" />
    <title>验证验证码信息</title>
    <script>
        function change(){
            document.getElementById("image_checkcode").src=&#39;./store.php?r=&#39;+Math.random();  
        }    </script></head><body><form action="./form.php" method="post"><p>验证码图片:</p>
        <img  src="/static/imghwm/default1.png"  data-src="./store.php?r=<?php echo rand();? alt="詳細介紹PHP驗證碼實作的原理" >"  class="lazy"  id="image_checkcode"    />
        <a href="javascript:void(0)" onclick="change()">看不清楚</a>
        <br/>请输入验证码<input type="text" name="checkcode" /><br /><p>
        <input type="submit" value="提交" /></p></form>
        </body></html>

總結

最後,來個總結吧。

  • 使用php製作驗證碼需要gd拓展的支援。

  • 使用imagecreatetruecolor方法產生背景色,並用imagefill填滿一個由imagecolorallocate產生的顏色。

  • 使用imagestring來實作驗證碼和背景圖的結合

  • 使用imagesetpixel來新增幹擾點

  • #使用imageline來新增幹擾線

  • 使用session之前要在開頭開啟session_start()方法

  • 使用JavaScript來動態的修改驗證碼的src,來滿足使用者「換一張」的需求。

  • #

以上是詳細介紹PHP驗證碼實作的原理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版

SublimeText3 Mac版

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

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器