拓展
我們需要開啟gd拓展,可以使用下面的程式碼來檢視是否開啟gd拓展。
<?phpecho "Hello World!!!!";echo phpinfo();?>
然後在瀏覽器上Ctrl+F
查找gd選項即可驗證自己有沒有裝這個拓展,如果沒有的話,還需要自己全裝一下這個拓展。
背景圖
imagecreatetruecolor
預設產生黑色背景
<?php // 使用gd的 imagecreatetruecolor();创建一张背景图 $image = imagecreatetruecolor(100,30);// 在显示这张图片的时候一定要先声明头信息 header('content-type:image/png'); 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('content-type:image/png'); 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 = 'abcdefghjkmnopqrstuvwxyz23456789ABCDEFGHJKLMNOPQRSTUVWXYZ'; $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('content-type:image/png'); imagepng($image);// 释放资源,销毁执行对象imagedestroy($image);
使用驗證碼
開啟session的時機
## 驗證的原理 驗證的過程就是客戶端輸入的驗證碼和存在於session域中的驗證碼進行比較。即:注意: 開啟session一定要在開始的地方
if(isset($_REQUEST['checkcode'])){ session_start(); if($_REQUEST['checkcode']==$_SESSION['checkcode']){ echo "<font color='green'>Success!</font>"; }else{ echo "<font color='red'>Failed!</font>"; } exit(); }優化驗證但是簡單的這樣驗證有一點不好的地方,那就是字母的大小寫容易出錯。所以我們要做一下轉換,將使用者輸入的數值全部變成小寫的。
if(strtolower($_REQUEST['checkcode'])==$_SESSION['checkcode']){···}小案例產生驗證碼
表單驗證
<?php
header("Content-Type:text/html;charset=utf8");
if(isset($_REQUEST['checkcode'])){
session_start();
if(strtolower($_REQUEST['checkcode'])==$_SESSION['checkcode']){
echo "<font color='green'>Success!</font>";
}else{
echo "<font color='red'>Failed!</font>";
}
exit();
}?>
<!DOCTYPE html><html><head>
<meta charset="utf-8" />
<title>验证验证码信息</title>
<script>
function change(){
document.getElementById("image_checkcode").src='./store.php?r='+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中文網其他相關文章!

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

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

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

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

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

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

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

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

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

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器