本篇文章為大家展示了利用php怎么編寫一個驗證碼小程序,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站主營南明網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,App定制開發(fā),南明h5微信平臺小程序開發(fā)搭建,南明網(wǎng)站營銷推廣歡迎南明等地區(qū)企業(yè)咨詢關(guān)于生成驗證碼的幾個小函數(shù)
range() //指定范圍輸出一個數(shù)組
a) 如: range(1,9)
array_merge()//合并數(shù)組
a) array_merge(數(shù)組1,數(shù)組2….)
array_rand(數(shù)組,數(shù)量)
a) 隨機從數(shù)組中取出幾個下標返回一個數(shù)組
shuffle(數(shù)組)//將再一次打亂數(shù)組中元素
mt_rand(指定一個范圍) //生成一個更好的隨機數(shù)
如: mt_rand(1,5) //生成一個在1-5之間的任意數(shù)
生成驗證碼代碼
<?php
$arr1=range('a', 'z');//指定范圍輸出一個數(shù)組
$arr2=range('A', 'Z');
$arr3=range(1,9);
$arr=array_merge($arr1,$arr2,$arr3); //合并數(shù)組
$index = array_rand($arr,5); //在$arr中隨機取5個數(shù),返回值是$arr的下標
Shuffle($index);
$code = '';//定義一個空的字符串來存儲生成的驗證碼用'點'來進行拼接
foreach ($index as $key => $value) {//遍歷數(shù)組
$code.= $arr[$value];//根據(jù)下標取數(shù)組中的值
}
var_dump($code);
?>運行結(jié)果截圖

完善:要把驗證碼添加到圖像中這樣的驗證碼才逼真
在完善之前先介紹有關(guān)圖像創(chuàng)建的大致步驟
創(chuàng)建圖像
方法一: 創(chuàng)建一個真彩色圖像 (空畫布)
imagecreatetruecolor(width, height) //創(chuàng)建一個真彩色圖像
說明:
width : 畫布的寬度(像素)
height: 畫布的高度(像素)
返回值為圖像資源

注意:

為真彩色圖像: 填充顏色
imagefill(image, x, y, color) //為圖像資源填充顏色
說明:
image //圖像資源
x,y,填充的坐標點(注意:填充的與此點最接近的顏色)
color; //用什么顏色來填充
為真彩色圖像: 分配顏色
imagecolorallocate(image, red, green, blue)
說明:
image //圖像資源
red: //紅顏色(0-255) 或 0x(00-ff) //即十六進制來表示 (0xff就是255)
green//綠顏色(0-255)
blue //藍顏色(0-255)
imagefill和imagecolorallocate的代碼演示
在沒有給畫布填充顏色時的效果

給畫布填充顏色時的效果和代碼
<?php
//創(chuàng)建圖像資源(空白畫布)默認顯示為黑色
$image = imagecreatetruecolor(300, 400);
//1.image //圖像資源
//2.red: //紅顏色(0-255) 或 0x(00-ff) //即十六進制來表示 (0xff就是255)
//3.green//綠顏色(0-255)
//4.blue //藍顏色(0-255)
$color = imagecolorallocate($image, 255, 0, 0);
//1.image //圖像資源
//2.x,y,填充的坐標點(注意:填充的與此點最接近的顏色)
//3.color; //用什么顏色來填充
imagefill($image, 0, 0, $color);
//輸出圖像
header('content-type:image/jpeg');
imagejpeg($image);
//銷毀圖像資源
imagedestroy($image);
?>結(jié)果截圖;

輸出圖像(以jpeg為例)
輸出圖像到瀏覽器
a) header('content-type:image/jpeg'); //設(shè)置將圖像通過瀏覽來查看
b) imagejpeg(圖像資源)
按文件進行輸出圖像
a) imagejpeg(圖像資源,'圖像路徑',圖像的質(zhì)量) //質(zhì)量取值0-100
b) 注意:

注意:只能jpeg格式才有質(zhì)量這個參數(shù).
銷毀圖像
imagedestroy($image); //銷毀圖像,釋放內(nèi)存資源.
注意: 當前生成幾個圖像資源,就銷毀幾個.
驗證碼的整個代碼:
<?php
//實例:讓文本居于圖像的正中
//創(chuàng)建圖像資源(空白的畫布)
$image = imagecreatetruecolor(100, 50);
$color = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
//為圖像資源填充顏色
imagefill($image, 0, 0, $color);
//繪制圖像
$font = 5;
//驗證碼的開始
$arr1 = range('a','z');
$arr3 = range('A','Z');
$arr2 = range(1,9);
//array_merge — 合并一個或多個數(shù)組
$arr = array_merge($arr1,$arr2,$arr3);
$index = array_rand($arr,5); //隨機從原數(shù)組中找出5個下標
$string = '';
foreach ($index as $value) { //$value 兩個功能,即是$index中的值,又是$arr中的下標
$string .= $arr[$value]; //將得到字符進行連接
}
//驗證碼的結(jié)束
//mt_rand — 生成更好的隨機數(shù)
//echo mt_rand(1,5);die;
//加入點干擾
$pointcolor = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
//循環(huán)創(chuàng)建1000個干擾點
for ($i=0; $i <1000 ; $i++) {
imagesetpixel($image, mt_rand(0,imagesx($image)), mt_rand(0,imagesy($image)), $pointcolor);
}
//加入線的干擾
$lintecolor = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
// 循環(huán)創(chuàng)建50個線干擾
for ($i=0; $i <50 ; $i++) {
imageline($image, mt_rand(0,imagesx($image)), mt_rand(0,imagesy($image)), mt_rand(0,imagesx($image)), mt_rand(0,imagesy($image)) ,$lintecolor);
}
//一個字符的寬度 imagefontwidth($font)
//字符串的個數(shù): strlen(字符串)
//一個字符的寬度*字符串的個數(shù)
//所有字符串寬度和= 一個字符的寬度*字符串的個數(shù)
//$x = (畫布的寬度-所有字符串寬度和)/2
$x = (imagesx($image)-imagefontwidth($font)*strlen($string))/2;
//$y = (畫布的高度-字符的高度)/2;
//字符的高度: imagefontheight($font)
$y = (imagesy($image)-imagefontheight($font))/2;
$stringcolor = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
imagestring($image, $font, $x, $y, $string, $stringcolor);
//輸出圖像
header('content-type:image/jpeg'); //設(shè)置將圖像通過瀏覽來查看
imagejpeg($image,'',100); //將圖像資源輸出
//銷毀圖像資源
imagedestroy($image); //銷毀圖像理解代碼中的一些函數(shù)
加入干擾的點
imagesetpixel(image, x, y, color)
說明:x,y 一個點的坐標

加入干擾的線
imageline(image, x1, y1, x2, y2, color)
說明: x1,y1是線的一個端點坐標; x2,y2是線的另一個端口的坐標; 由兩點畫一條線

讓驗證碼居于圖像的正中
imagefontheight(font)獲取字體的高度: imagefontwidth(font)獲取字體的寬度: strlen(字符串)//獲取字符串的長度 imagesx(image) //獲取畫布的寬度 imagesy(image) //獲取畫布的高度

最后運行結(jié)果

再次完善(和html代碼結(jié)合起來)
Html代碼

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form name='frm' method='post' action=''> <table width="30%" border="2" align="center" rules="all" cellpadding="10"> <tr> <th colspan="2">請輸入信息</th> </tr> <tr> <th>姓名:</th> <th><input type="text" name="username"></input></th> </tr> <tr> <th>密碼:</th> <th><input type="password" name="userpwd"></input></th> </tr> <tr> 555556 <th>驗證碼</th> <th><input type = 'text' name = 'checkcode'></input><img src="21.php" onclick="this.src='21.php'?+Math.random()"></th> </tr> <tr> <th colspan="2"><input type="submit" name="submit" value="提交"></input></th> </tr> </table> </form> </body> </html>
理解;

上述內(nèi)容就是利用php怎么編寫一個驗證碼小程序,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:利用php怎么編寫一個驗證碼小程序-創(chuàng)新互聯(lián)
文章地址:http://chinadenli.net/article0/cohoio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、電子商務(wù)、營銷型網(wǎng)站建設(shè)、面包屑導(dǎo)航、企業(yè)建站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)