php制作動(dòng)態(tài)隨機(jī)驗(yàn)證碼
驗(yàn)證碼(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自動(dòng)區(qū)分計(jì)算機(jī)和人類(lèi)的圖靈測(cè)試)的縮寫(xiě),是一種區(qū)分用戶(hù)是計(jì)算機(jī)還是人的公共全自動(dòng)程序。可以防止:惡意破解密碼、刷票、論壇灌水,有效防止某個(gè)黑客對(duì)某一個(gè)特定注冊(cè)用戶(hù)用特定程序暴力破解方式進(jìn)行不斷的登陸嘗試,實(shí)際上用驗(yàn)證碼是現(xiàn)在很多網(wǎng)站通行的方式,我們利用比較簡(jiǎn)易的方式實(shí)現(xiàn)了這個(gè)功能。
這個(gè)問(wèn)題可以由計(jì)算機(jī)生成并評(píng)判,但是必須只有人類(lèi)才能解答。由于計(jì)算機(jī)無(wú)法解答CAPTCHA的問(wèn)題,所以回答出問(wèn)題的用戶(hù)就可以被認(rèn)為是人類(lèi)。
Php制作動(dòng)態(tài)驗(yàn)證碼是基于php的圖像處理,下面首先介紹一下php的圖像處理。
一.php圖像處理簡(jiǎn)介
在PHP5中,動(dòng)態(tài)圖象的處理要比以前容易得多。PHP5在php.ini文件中包含了GD擴(kuò)展包,只需去掉GD擴(kuò)展包的相應(yīng)注釋就可以正常使用了。PHP5包含的GD庫(kù)正是升級(jí)的GD2庫(kù),其中包含支持真彩圖像處理的一些有用的JPG功能。
一般生成的圖形,通過(guò)PHP的文檔格式存放,但可以通過(guò)HTML的圖片插入方式SRC來(lái)直接獲取動(dòng)態(tài)圖形。比如,驗(yàn)證碼、水印、微縮圖等。
創(chuàng)建圖像的一般流程:
1).設(shè)定標(biāo)頭,告訴瀏覽器你要生成的MIME類(lèi)型。
2).創(chuàng)建一個(gè)圖像區(qū)域,以后的操作都將基于此圖像區(qū)域。
3).在空白圖像區(qū)域繪制填充背景。
4).在背景上繪制圖形輪廓輸入文本。
5).輸出最終圖形。
6).清除所有資源。
7).其他頁(yè)面調(diào)用圖像。
第一步,設(shè)置文件MIME類(lèi)型,輸出類(lèi)型 將輸出類(lèi)型改成圖像流
header('Content-Type: image/png;');
一般生成的圖像可以是png,jpeg,gif,wbmp
第二步,創(chuàng)建一個(gè)圖形區(qū)域,圖像背景
imagecreatetruecolor() 返回一個(gè)圖像標(biāo)識(shí)符,代表了一幅大小為 x_size 和 y_size 的黑色圖像。語(yǔ)法:resource imagecreatetruecolor ( int $width , int $height )
$im = imagecreatetruecolor(200,200);
第三步,在空白圖像區(qū)域繪制填充背景
要有顏色填充器;imagecolorallocate -- 為一幅圖像分配顏色;語(yǔ)法:int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
$blue = imagecolorallocate($im,0,102,255);
將這個(gè)blue顏色填充到背景上去;imagefill -- 區(qū)域填充;語(yǔ)法:bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill($im,0,0,$blue);
第四步,在藍(lán)色的背景上輸入一些線(xiàn)條,文字等
顏色填充器
$white = imagecolorallocate($im,255,255,255);
畫(huà)兩條線(xiàn)段:imageline
imageline() 用 color 顏色在圖像 image 中從坐標(biāo) x1,y1 到 x2,y2(圖像左上角為 0, 0)畫(huà)一條線(xiàn)段。語(yǔ)法:bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline($im,0,0,200,200,$white);
imageline($im,200,0,0,200,$white);
水平地畫(huà)一行字符串:imagestring
imagestring() 用 col 顏色將字符串 s 畫(huà)到 image 所代表的圖像的 x,y 坐標(biāo)處(這是字符串左上角坐標(biāo),整幅圖像的左上角為 0,0)。如果font 是 1,2,3,4 或 5,則使用內(nèi)置字體。語(yǔ)法:bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
imagestring($im,5,66,20,'jingwhale',$white);
第五步,輸出最終圖形
imagepng() 將 GD 圖像流(image)以 PNG 格式輸出到標(biāo)準(zhǔn)輸出(通常為瀏覽器),或者如果用 filename 給出了文件名則將其輸出到該文件。語(yǔ)法:bool imagepng ( resource $image [, string $filename ] )
imagepng($im);
第六步,我要將所有的資源全部清空
imagedestroy() 釋放與 image 關(guān)聯(lián)的內(nèi)存。語(yǔ)法:bool imagedestroy ( resource $image )
imagedestroy($im);
其他頁(yè)面(html)調(diào)用創(chuàng)建的圖形
<img src="Demo4.php" alt="PHP創(chuàng)建的圖片" />
示例代碼如下:
<?php
//第一步,設(shè)置文件MIME類(lèi)型
header('Content-Type: image/png;');
//第二步,創(chuàng)建一個(gè)圖形區(qū)域,圖像背景
$im = imagecreatetruecolor(200,200);
//第三步,在空白圖像區(qū)域繪制填充背景
$blue = imagecolorallocate($im,0,102,255);
imagefill($im,0,0,$blue);
//第四步,在藍(lán)色的背景上輸入一些線(xiàn)條,文字等
$white = imagecolorallocate($im,255,255,255);
imageline($im,0,0,200,200,$white);
imageline($im,200,0,0,200,$white);
imagestring($im,5,66,20,'Jing.Whale',$white);
//第五步,輸出最終圖形
imagepng($im);
//第六步,我要將所有的資源全部清空
imagedestroy($im);
?>
顯示效果:
二.創(chuàng)建動(dòng)態(tài)驗(yàn)證碼
附:代碼源地址https://github.com/cnblogs-/php-captcha
1. 創(chuàng)建帶驗(yàn)證碼的圖片,并模糊背景
隨機(jī)碼采用16進(jìn)制;模糊背景即在圖片背景加上線(xiàn)條、雪花等。
1)創(chuàng)建隨機(jī)碼
for ($i=0;$i<$_rnd_code;$i++) {
$_nmsg .= dechex(mt_rand(0,15));
}
string dechex ( int $number ),返回一字符串,包含有給定 number 參數(shù)的十六進(jìn)制表示。
2)保存在session
$_SESSION['code'] = $_nms
3)創(chuàng)建圖片
//創(chuàng)建一張圖像
$_img = imagecreatetruecolor($_width,$_height);
//白色
$_white = imagecolorallocate($_img,255,255,255);
//填充
imagefill($_img,0,0,$_white);
if ($_flag) {
//黑色,邊框
$_black = imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
}
4)模糊背景
//隨機(jī)畫(huà)出6個(gè)線(xiàn)條
for ($i=0;$i<6;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}
//隨機(jī)雪花
for ($i=0;$i<100;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
}
5)輸出及銷(xiāo)毀
//輸出驗(yàn)證碼
for ($i=0;$i<strlen($_SESSION['code']);$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color);
}
//輸出圖像
header('Content-Type: image/png');
imagepng($_img);
//銷(xiāo)毀
imagedestroy($_img);
將其封裝在global.func.php全局函數(shù)庫(kù)中,函數(shù)名為_(kāi)code(),以便調(diào)用。我們將設(shè)置$_width ,$_height ,$_rnd_code,$_flag 四個(gè)參數(shù),以增強(qiáng)函數(shù)的靈活性。
* @param int $_width 驗(yàn)證碼的長(zhǎng)度:如果要6位長(zhǎng)度推薦75+50;如果要8位,推薦75+50+50,依次類(lèi)推
* @param int $_height 驗(yàn)證碼的高度
* @param int $_rnd_code 驗(yàn)證碼的位數(shù)
* @param bool $_flag 驗(yàn)證碼是否需要邊框:true有邊框, false無(wú)邊框(默認(rèn))
封裝后的代碼如下:
<?php
/**
* [verification-code] (C)2015-2100 jingwhale.
*
* This is a freeware
* $Id: global.func.php 2015-02-05 20:53:56 jingwhale$
*/
/**
* _code()是驗(yàn)證碼函數(shù)
* @access public
* @param int $_width 驗(yàn)證碼的長(zhǎng)度:如果要6位長(zhǎng)度推薦75+50;如果要8位,推薦75+50+50,依次類(lèi)推
* @param int $_height 驗(yàn)證碼的高度
* @param int $_rnd_code 驗(yàn)證碼的位數(shù)
* @param bool $_flag 驗(yàn)證碼是否需要邊框:true有邊框, false無(wú)邊框(默認(rèn))
* @return void 這個(gè)函數(shù)執(zhí)行后產(chǎn)生一個(gè)驗(yàn)證碼
*/
function _code($_width = 75,$_height = 25,$_rnd_code = 4,$_flag = false) {
//創(chuàng)建隨機(jī)碼
for ($i=0;$i<$_rnd_code;$i++) {
$_nmsg .= dechex(mt_rand(0,15));
}
//保存在session
$_SESSION['code'] = $_nmsg;
//創(chuàng)建一張圖像
$_img = imagecreatetruecolor($_width,$_height);
//白色
$_white = imagecolorallocate($_img,255,255,255);
//填充
imagefill($_img,0,0,$_white);
if ($_flag) {
//黑色,邊框
$_black = imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
}
//隨即畫(huà)出6個(gè)線(xiàn)條
for ($i=0;$i<6;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}
//隨即雪花
for ($i=0;$i<100;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
}
//輸出驗(yàn)證碼
for ($i=0;$i<strlen($_SESSION['code']);$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color);
}
//輸出圖像
header('Content-Type: image/png');
imagepng($_img);
//銷(xiāo)毀
imagedestroy($_img);
}
?>
2.創(chuàng)建驗(yàn)證機(jī)制
創(chuàng)建php驗(yàn)證頁(yè)面,通過(guò)session來(lái)檢驗(yàn)驗(yàn)證碼是否一致。
1)創(chuàng)建verification-code.php驗(yàn)證頁(yè)面
<?php
/**
* [verification-code] (C)2015-2100 jingwhale.
*
* This is a freeware
* $Id: verification-code.php 2015-02-05 20:53:56 jingwhale$
*/
//設(shè)置字符集編碼
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>verification code</title>
<link rel="stylesheet" type="text/css" href="style/basic.css" />
</head>
<body>
<div id="testcode">
<form method="post" name="verification" action="verification-code.php?action=verification">
<dl>
<dd>驗(yàn)證碼:<input type="text" name="code" class="code" /><img src="codeimg.php" id="codeimg" /></dd>
<dd><input type="submit" class="submit" value="驗(yàn)證" /></dd>
</dl>
</form>
</div>
</body>
</html>
顯示如下:
2)創(chuàng)建產(chǎn)生驗(yàn)證碼圖片頁(yè)面
創(chuàng)建codeimg.php為verification-code.php html代碼里的img提供驗(yàn)證碼圖片
首先必須在codeimg.php頁(yè)面開(kāi)啟session;
其次,將我們封裝好的global.func.php全局函數(shù)庫(kù)引入進(jìn)來(lái);
最后,運(yùn)行_code();
<?php
/**
* [verification-code] (C)2015-2100 jingwhale.
*
* This is a freeware
* $Id: codeimg.php 2015-02-05 20:53:56 jingwhale$
*/
//開(kāi)啟session
session_start();
//引入全局函數(shù)庫(kù)(自定義)
require dirname(__FILE__).'/includes/global.func.php';
//運(yùn)行驗(yàn)證碼函數(shù)。通過(guò)數(shù)據(jù)庫(kù)的_code方法,設(shè)置驗(yàn)證碼的各種屬性,生成圖片
_code(125,25,6,false);
?>
3)創(chuàng)建session檢驗(yàn)機(jī)制
首先必須在verification-code.php頁(yè)面也開(kāi)啟session;
其次,設(shè)計(jì)提交驗(yàn)證碼的方式,本文以get方式提交,當(dāng)action=verification時(shí)提交成功;
最后,創(chuàng)建驗(yàn)證函數(shù),原理是將客戶(hù)端用戶(hù)提交的驗(yàn)證碼同服務(wù)器codeimg.php中session的驗(yàn)證碼是否一致;這里有一個(gè)js彈窗函數(shù)_alert_back(),我們也把它封裝在global.func.php里;
修改verification-code.php中php代碼如下:
<?php
/**
* [verification-code] (C)2015-2100 jingwhale.
*
* This is a freeware
* $Id: verification-code.php 2015-02-05 20:53:56 jingwhale$
*/
//設(shè)置字符集編碼
header('Content-Type: text/html; charset=utf-8');
//開(kāi)啟session
session_start();
//引入全局函數(shù)庫(kù)(自定義)
require dirname(__FILE__).'/includes/global.func.php';
//檢驗(yàn)驗(yàn)證碼
if ($_GET['action'] == 'verification') {
if (!($_POST['code'] == $_SESSION['code'])) {
_alert_back('驗(yàn)證碼不正確!');
}else{
_alert_back('驗(yàn)證碼通過(guò)!');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>verification code</title>
<link rel="stylesheet" type="text/css" href="style/basic.css" />
<script type="text/javascript" src="js/codeimg.js"></script>
</head>
<body>
<div id="testcode">
<form method="post" name="verification" action="verification-code.php?action=verification">
<dl>
<dd>驗(yàn)證碼:<input type="text" name="code" class="code" /><img src="codeimg.php" id="codeimg" /></dd>
<dd><input type="submit" class="submit" value="驗(yàn)證" /></dd>
</dl>
</form>
</div>
</body>
</html>
3.實(shí)現(xiàn)點(diǎn)擊驗(yàn)證碼圖片更新驗(yàn)證碼
上面若想實(shí)現(xiàn)驗(yàn)證碼更新,必須刷新頁(yè)面;我們寫(xiě)一個(gè)codeimg.js函數(shù)實(shí)現(xiàn)點(diǎn)擊驗(yàn)證碼圖片更新驗(yàn)證碼
window.onload = function () {
var code = document.getElementById('codeimg');//通過(guò)id找到html中img標(biāo)簽
code.onclick = function () {//為標(biāo)簽添加點(diǎn)擊事件
this.src='codeimg.php?tm='+Math.random();//修改時(shí)間,重新指向codeimg.php
};
}
然后在verification-code.php html代碼head里<link>它即可。
- PHP封裝函數(shù)實(shí)現(xiàn)生成隨機(jī)的字符串驗(yàn)證碼
- 生成隨機(jī)字符串和驗(yàn)證碼的類(lèi)的PHP實(shí)例
- PHP 動(dòng)態(tài)隨機(jī)生成驗(yàn)證碼類(lèi)代碼
- php 生成隨機(jī)驗(yàn)證碼圖片代碼
- php圖片驗(yàn)證碼代碼
- PHP生成圖片驗(yàn)證碼、點(diǎn)擊切換實(shí)例
- PHP使用CURL實(shí)現(xiàn)對(duì)帶有驗(yàn)證碼的網(wǎng)站進(jìn)行模擬登錄的方法
- ThinkPHP驗(yàn)證碼使用簡(jiǎn)明教程
- 一個(gè)經(jīng)典的PHP驗(yàn)證碼類(lèi)分享
- PHP生成各種隨機(jī)驗(yàn)證碼的方法總結(jié)【附demo源碼】
相關(guān)文章
laravel利用中間件做防非法登錄和權(quán)限控制示例
今天小編就為大家分享一篇laravel利用中間件做防非法登錄和權(quán)限控制示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10php微信公眾平臺(tái)開(kāi)發(fā)之獲取用戶(hù)基本信息
本文介紹如何獲得微信公眾平臺(tái)關(guān)注用戶(hù)的基本信息,包括昵稱(chēng)、頭像、性別等基本信息。下面小編把最近整理有關(guān)php微信公眾平臺(tái)開(kāi)發(fā)之獲取用戶(hù)基本信息的相關(guān)內(nèi)容分享給大家,有需要的朋友可以參考下2015-08-08PHP定時(shí)執(zhí)行任務(wù)實(shí)現(xiàn)方法詳解(Timer)
這篇文章主要介紹了PHP定時(shí)執(zhí)行任務(wù)實(shí)現(xiàn)方法詳解,定時(shí)任務(wù)在web應(yīng)用程序中比較常見(jiàn),實(shí)現(xiàn)定時(shí)任務(wù)主要有兩種方案:1)使用Crontab命令,2)配合使用ignore_user_abort()和set_time_limit(),有需要的朋友可以來(lái)借鑒下。2015-07-07PHP時(shí)間戳 strtotime()使用方法和技巧
php strtotime()解釋如何使用,看了下面的文章就要以學(xué)習(xí)到了。下面還有php文檔函數(shù)解釋。2013-10-10利用PHP訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)_實(shí)現(xiàn)分頁(yè)功能與多條件查詢(xún)功能的示例
下面小編就為大家?guī)?lái)一篇利用PHP訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)_實(shí)現(xiàn)分頁(yè)功能與多條件查詢(xún)功能的示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09PHP+Mysql+jQuery實(shí)現(xiàn)發(fā)布微博程序 php篇
這篇文章主要介紹了PHP+Mysql+jQuery實(shí)現(xiàn)發(fā)布微博程序,重要介紹后臺(tái)是如何處理前臺(tái)提交的數(shù)據(jù),并返回結(jié)果的,需要的朋友可以參考下2015-10-10