php生成圖片驗(yàn)證碼-附五種驗(yàn)證碼
以前輸出驗(yàn)證碼的時(shí)候用過一個(gè)方法,在前臺用JS生成驗(yàn)證碼字符串,再傳遞到后臺用PHP輸出驗(yàn)證碼圖像。這樣在驗(yàn)證時(shí)就不需要使用$_SESSION傳遞驗(yàn)證碼的值,直接用JS比較生成的字符串和輸入的字符串是否相等即可。
本文以實(shí)例演示5種驗(yàn)證碼,并介紹生成驗(yàn)證碼的函數(shù)。PHP生成驗(yàn)證碼的原理:通過GD庫,生成一張帶驗(yàn)證碼的圖片,并將驗(yàn)證碼保存在Session中。
1、HTML
5中驗(yàn)證碼HTML代碼如下:
<div class="demo"> <h3>1、數(shù)字驗(yàn)證碼</h3> <p>驗(yàn)證碼:<input type="text" class="input" id="code_num" name="code_num" maxlength="4" /> <img src="code_num.php" id="getcode_num" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p> <p><input type="button" class="btn" id="chk_num" value="提交" /></p> </div> <div class="demo">
<h3>2、數(shù)字+字母驗(yàn)證碼</h3> <p>驗(yàn)證碼:<input type="text" class="input" id="code_char" maxlength="4" /> <img src="code_char.php" id="getcode_char" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p> <p><input type="button" class="btn" id="chk_char" value="提交" /></p> </div> <div class="demo">
<h3>3、中文驗(yàn)證碼</h3> <p>驗(yàn)證碼:<input type="text" class="input" id="code_zh" maxlength="4" /> <img src="code_zh.php" id="getcode_zh" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p> <p><input type="button" class="btn" id="chk_zh" value="提交" /></p> </div> <div class="demo">
<h3>4、仿google驗(yàn)證碼</h3> <p>驗(yàn)證碼:<input type="text" class="input" id="code_gg" maxlength="4" /> <img src="code_gg.php" id="getcode_gg" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p> <p><input type="button" class="btn" id="chk_gg" value="提交" /></p> </div> <div class="demo">
<h3>5、算術(shù)驗(yàn)證碼</h3> <p>驗(yàn)證碼:<input type="text" class="input" id="code_math" maxlength="4" /> <img src="code_math.php" id="getcode_math" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p> <p><input type="button" class="btn" id="chk_math" value="提交" /></p> </div>
2、js驗(yàn)證
$(function() { $("#getcode_num").click(function() { //數(shù)字驗(yàn)證 $(this).attr("src", 'code_num.php?' + Math.random()); }); $("#chk_num").click(function() { var code_num = $("#code_num").val(); $.post("chk_code.php?act=num", { code: code_num }, function(msg) { if (msg == 1) { alert("驗(yàn)證碼正確!"); } else { alert("驗(yàn)證碼錯(cuò)誤!"); } }); });
//數(shù)字+字母驗(yàn)證
$("#getcode_char").click(function() { $(this).attr("src", 'code_char.php?' + Math.random()); }); $("#chk_char").click(function() { var code_char = $("#code_char").val(); $.post("chk_code.php?act=char", { code: code_char }, function(msg) { if (msg == 1) { alert("驗(yàn)證碼正確!"); } else { alert("驗(yàn)證碼錯(cuò)誤!"); } }); });
//中文驗(yàn)證碼
$("#getcode_zh").click(function() { $(this).attr("src", 'code_zh.php?' + Math.random()); }); $("#chk_zh").click(function() { var code_zh = escape($("#code_zh").val()); $.post("chk_code.php?act=zh", { code: code_zh }, function(msg) { if (msg == 1) { alert("驗(yàn)證碼正確!"); } else { alert("驗(yàn)證碼錯(cuò)誤!"); } }); });
//google驗(yàn)證
$("#getcode_gg").click(function() { $(this).attr("src", 'code_gg.php?' + Math.random()); }); $("#chk_gg").click(function() { var code_gg = $("#code_gg").val(); $.post("chk_code.php?act=gg", { code: code_gg }, function(msg) { if (msg == 1) { alert("驗(yàn)證碼正確!"); } else { alert("驗(yàn)證碼錯(cuò)誤!"); } }); });
//算術(shù)驗(yàn)證
$("#getcode_math").click(function() { $(this).attr("src", 'code_math.php?' + Math.random()); }); $("#chk_math").click(function() { var code_math = $("#code_math").val(); $.post("chk_code.php?act=math", { code: code_math }, function(msg) { if (msg == 1) { alert("驗(yàn)證碼正確!"); } else { alert("驗(yàn)證碼錯(cuò)誤!"); } }); }); });
3、PHP生成驗(yàn)證碼
session_start(); getCode(4,60,20); function getCode($num,$w,$h) { $code = ""; for ($i = 0; $i < $num; $i++) { $code .= rand(0, 9); } //4位驗(yàn)證碼也可以用rand(1000,9999)直接生成 //將生成的驗(yàn)證碼寫入session,備驗(yàn)證時(shí)用 $_SESSION["helloweba_num"] = $code; //創(chuàng)建圖片,定義顏色值 header("Content-type: image/PNG"); $im = imagecreate($w, $h); $black = imagecolorallocate($im, 0, 0, 0); $gray = imagecolorallocate($im, 200, 200, 200); $bgcolor = imagecolorallocate($im, 255, 255, 255); //填充背景 imagefill($im, 0, 0, $gray); //畫邊框 imagerectangle($im, 0, 0, $w-1, $h-1, $black); //隨機(jī)繪制兩條虛線,起干擾作用 $style = array ($black,$black,$black,$black,$black, $gray,$gray,$gray,$gray,$gray ); imagesetstyle($im, $style); $y1 = rand(0, $h); $y2 = rand(0, $h); $y3 = rand(0, $h); $y4 = rand(0, $h); imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); //在畫布上隨機(jī)生成大量黑點(diǎn),起干擾作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $w), rand(0, $h), $black); } //將數(shù)字隨機(jī)顯示在畫布上,字符的水平間距和位置都按一定波動(dòng)范圍隨機(jī)生成 $strx = rand(3, 8); for ($i = 0; $i < $num; $i++) { $strpos = rand(1, 6); imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); $strx += rand(8, 12); } imagepng($im);//輸出圖片 imagedestroy($im);//釋放圖片所占內(nèi)存 }
以上內(nèi)容就是php生成圖片驗(yàn)證碼-附五種驗(yàn)證碼的全部內(nèi)容,希望大家喜歡。
- thinkphp整合系列之極驗(yàn)滑動(dòng)驗(yàn)證碼geetest功能
- Ajax和PHP正則表達(dá)式驗(yàn)證表單及驗(yàn)證碼
- PHP實(shí)現(xiàn)登陸表單提交CSRF及驗(yàn)證碼
- Ajax提交表單時(shí)驗(yàn)證碼自動(dòng)驗(yàn)證 php后端驗(yàn)證碼檢測
- php生成圖形驗(yàn)證碼幾種方法小結(jié)
- php 生成隨機(jī)驗(yàn)證碼圖片代碼
- PHP制作圖形驗(yàn)證碼代碼分享
- PHP5中GD庫生成圖形驗(yàn)證碼(有漢字)
- PHP生成Gif圖片驗(yàn)證碼
- 如何用php生成扭曲及旋轉(zhuǎn)的驗(yàn)證碼圖片
- php+js實(shí)現(xiàn)的拖動(dòng)滑塊驗(yàn)證碼驗(yàn)證表單操作示例【附源碼下載】
相關(guān)文章
php curl登陸qq后獲取用戶信息時(shí)證書錯(cuò)誤
這篇文章主要介紹了php curl登陸qq后獲取用戶信息時(shí)證書錯(cuò)誤,需要的朋友可以參考下2015-02-02初識通用數(shù)據(jù)庫操作類——前端easyui-datagrid,form(php)
這篇文章主要介紹了初識通用數(shù)據(jù)庫操作類——前端easyui-datagrid,form(php),實(shí)現(xiàn)代碼比較簡單,有需要的小伙伴歡迎來參考2015-07-07Yii中CArrayDataProvider和CActiveDataProvider區(qū)別實(shí)例分析
這篇文章主要介紹了Yii中CArrayDataProvider和CActiveDataProvider區(qū)別,結(jié)合實(shí)例形式分析了Yii中CArrayDataProvider和CActiveDataProvider的具體功能與用法,需要的朋友可以參考下2016-03-03阿里云PHP SMS短信服務(wù)驗(yàn)證碼發(fā)送方法
這篇文章主要介紹了阿里云PHP SMS短信服務(wù)驗(yàn)證碼發(fā)送方法,需要的朋友可以參考下2017-07-07Laravel中9個(gè)不經(jīng)常用的小技巧匯總
這篇文章主要給大家總結(jié)介紹了關(guān)于Laravel中9個(gè)不經(jīng)常用的小技巧,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Laravel具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04php 生成靜態(tài)頁面的辦法與實(shí)現(xiàn)代碼詳細(xì)版
首先說原理。查了那么多資料,發(fā)現(xiàn)不管用什么方法,原理都是一樣的。就是用程序讀取相應(yīng)的數(shù)據(jù)來替換模版中的變量,然后生成靜態(tài)頁。2010-02-02Thinkphp+smarty+uploadify實(shí)現(xiàn)無刷新上傳
這篇文章主要介紹了Thinkphp+smarty+uploadify實(shí)現(xiàn)無刷新上傳的方法,實(shí)例分析了php模板與js上傳插件結(jié)合實(shí)現(xiàn)無刷新上傳的相關(guān)技巧,需要的朋友可以參考下2015-07-07