在php中實(shí)現(xiàn)驗(yàn)證碼還是很方便的,關(guān)鍵點(diǎn)在于掌握php gd庫與session的用法。
縱觀網(wǎng)上php 生成驗(yàn)證碼的例子,無不是php gd庫與session相結(jié)合,并利用php 生成隨機(jī)數(shù)的方法來完成。
PHP驗(yàn)證碼,可以分為很多種,包括php 圖片驗(yàn)證碼,php 隨機(jī)驗(yàn)證碼,以及php 中文驗(yàn)證碼等,根據(jù)不同的應(yīng)用場(chǎng)合來使用不同的驗(yàn)證碼。
這里分享一個(gè)php數(shù)字驗(yàn)證碼,供大家參考。
4位數(shù)字驗(yàn)證碼
/*
*Filename:authpage.php
*/
session_start();
//srand((double)microtime()*1000000);
$authnum=$_SESSION['authnum'];
//驗(yàn)證用戶輸入是否和驗(yàn)證碼一致
if(isset($_POST['authinput']))
{
if(strcmp($_POST['authinput'],$_SESSION['authnum'])==0)
echo"驗(yàn)證成功!";
else
echo"驗(yàn)證失敗!";
}
//生成新的四位整數(shù)驗(yàn)證碼
//while(($authnum=rand()%10000)
?>
formaction=test4.phpmethod=post>
請(qǐng)輸入驗(yàn)證碼:inputtype=textname=authinputstyle="width:80px">br>
inputtype=submitname="驗(yàn)證"html' target='_blank'>value="提交驗(yàn)證碼">
inputtype=hiddenname=authnumvalue=echo$authnum;?>>
imgsrc=authimg.php?authnum=echo$authnum;?>>
table>
form>
authimg.php
php
//生成驗(yàn)證碼圖片
Header("Content-type:image/PNG");
srand((double)microtime()*1000000);//播下一個(gè)生成隨機(jī)數(shù)字的種子,以方便下面隨機(jī)數(shù)生成的使用
session_start();//將隨機(jī)數(shù)存入session中
$_SESSION['authnum']="";
$im=imagecreate(62,20);//制定圖片背景大小
$black=ImageColorAllocate($im,0,0,0);//設(shè)定三種顏色
$white=ImageColorAllocate($im,255,255,255);
$gray=ImageColorAllocate($im,200,200,200);
imagefill($im,0,0,$gray);//采用區(qū)域填充法,設(shè)定(0,0)
while(($authnum=rand()%100000)10000);
//將四位整數(shù)驗(yàn)證碼繪入圖片
$_SESSION['authnum']=$authnum;
imagestring($im,5,10,3,$authnum,$black);
//用col顏色將字符串s畫到image所代表的圖像的x,y座標(biāo)處(圖像的左上角為0,0)。
//如果font是1,2,3,4或5,則使用內(nèi)置字體
for($i=0;$i200;$i++)//加入干擾象素
{
$randcolor=ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im,rand()%70,rand()%30,$randcolor);
}
ImagePNG($im);
ImageDestroy($im);
?>
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選