基于PHP制作驗證碼
網(wǎng)站注冊、登錄又或者是留言頁面,都需要注冊碼來驗證當(dāng)前操作者的合法性,為了防止網(wǎng)站被機器惡意注冊。
生成驗證碼無非就那么幾個步驟,首先是獲取一個隨機字符串,然后創(chuàng)建一個布畫,將生成的字符串寫到布畫上,我們還可以在布畫上畫線畫雪花,現(xiàn)在帖一段生成驗證碼的代碼。
源代碼:
<?php
session_start(); //開啟session
//創(chuàng)建隨機碼,并保存在session中
for($i=0;$i<4;$i++)
{
$_nmsg.=dechex(mt_rand(0,15));
}
//保存到session中
$_SESSION['code']=$_nmsg;
//設(shè)置圖片長和高
$_width=75;
$_height=25;
//創(chuàng)建一張圖像
$_img=imagecreatetruecolor($_width,$_height);
//白色背景
$_white=imagecolorallocate($_img,255,255,255);
//填充到背景上
imagefill($_img,0,0,$_white);
//黑色邊框
$_black=imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
//隨即畫出5個線條
for($i=0;$i<5;$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<10;$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);
}
//輸出驗證碼
for($i=0;$i<strlen($_SESSION['code']);$i++)
{
imagestring($_img,5,10+$i*15,mt_rand(0,10),$_SESSION['code'][$i],$_blackr);
}
//輸出圖像
header('Content-Type:image/png');
imagepng($_img);
//銷毀圖像
imagedestroy($_img);
?>
代碼中將使用以下函數(shù):
mt_rand — 生成更好的隨機數(shù)
int mt_rand ([ int $min ], int $max )很多老的 libc 的隨機數(shù)發(fā)生器具有一些不確定和未知的特性而且很慢。PHP 的 rand() 函數(shù)默認(rèn)使用 libc 隨機數(shù)發(fā)生器。
mt_rand()函數(shù)是非正式用來替換它的。該函數(shù)用了Mersenne Twister中已知的特性作為隨機數(shù)發(fā)生器,它可以產(chǎn)生隨機數(shù)值的平均速度比 libc 提供的 rand() 快四倍。
dechex — 十進制轉(zhuǎn)換為十六進制返回一字符串,包含有給定 number參數(shù)的十六進制表示。所能轉(zhuǎn)換的最大數(shù)值為十進制的 4294967295,其結(jié)果為 "ffffffff"。
imagecreatetruecolor — 新建一個真彩色圖像
resource imagecreatetruecolor ( int $x_size , int $y_size )
imagecreatetruecolor() 返回一個圖像標(biāo)識符,代表了一幅大小為 x_size 和 y_size 的黑色圖像。
imagecolorallocate — 為一幅圖像分配顏色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一個標(biāo)識符,代表了由給定的 RGB 成分組成的顏色。red,green 和 blue 分別是所需要的顏色的紅,綠,藍(lán)成分。這些參數(shù)是 0 到 255 的整數(shù)或者十六進制的 0x00 到 0xFF。imagecolorallocate()必須被調(diào)用以創(chuàng)建每一種用在 image 所代表的圖像中的顏色。
imagefill — 區(qū)域填充
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image圖像的坐標(biāo) x,y(圖像左上角為 0, 0)處用 color顏色執(zhí)行區(qū)域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。
imagerectangle — 畫一個矩形
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
imagerectangle() 用 col 顏色在 image 圖像中畫一個矩形,其左上角坐標(biāo)為 x1, y1,右下角坐標(biāo)為 x2, y2。圖像的左上角坐標(biāo)為 0, 0。
imageline — 畫一條線段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color顏色在圖像 image 中從坐標(biāo) x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段。
imagestring — 水平地畫一行字符串
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
imagestring() 用 col顏色將字符串 s 畫到 image所代表的圖像的 x,y坐標(biāo)處(這是字符串左上角坐標(biāo),整幅圖像的左上角為 0,0)。如果 font 是 1,2,3,4 或 5,則使用內(nèi)置字體。
imagepng — 以 PNG 格式將圖像輸出到瀏覽器或文件
imagepng() 將 GD 圖像流(image)以 PNG 格式輸出到標(biāo)準(zhǔn)輸出(通常為瀏覽器),或者如果用 filename 給出了文件名則將其輸出到該文件。
imagedestroy — 銷毀一圖像
imagedestroy() 釋放與 image 關(guān)聯(lián)的內(nèi)存。
將源代碼保存為code.php是個php文件,我們該如何使用他呢?
imagepng已經(jīng)將這個php文件輸出成了png文件
直接調(diào)用就可以了
<img src="mycode.php"/>
如果要使用驗證碼,記得開啟session哦
<?php session_start(); echo $_SESSION['code']; ?>
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
關(guān)于PHP的相似度計算函數(shù):levenshtein的使用介紹
本篇文章小編將為大家介紹,關(guān)于PHP的相似度計算函數(shù) levenshtein的使用介紹,有需要的朋友可以參考一下2013-04-04
深入剖析瀏覽器退出之后php還會繼續(xù)執(zhí)行么
覽器退出之后php還會繼續(xù)執(zhí)行么?下面小編就為大家介紹一下究竟覽器退出之后php還會不會繼續(xù)執(zhí)行。一起跟隨小編過來看看吧2016-05-05
PHP getallheaders無法獲取自定義頭(headers)的問題
這篇文章主要介紹了PHP getallheaders無法獲取自定義頭(headers)的問題的相關(guān)資料,需要的朋友可以參考下2016-03-03

