php点击验证码,用PHP实现验证码功能
作者:hutuworm 來源:糊涂饞寺
牐犇殼埃不少網站為了防止用戶利用機器人自動注冊、登錄、灌水,都采用了
驗證碼技術。所謂驗證碼,就是將一串隨機產生的數字或符號,生成一幅圖片,
圖片里加上一些干擾象素(防止OCR),由用戶肉眼識別其中的驗證碼信息,輸
入表單提交網站驗證,驗證成功后才能使用某項功能。
牐犖頤欽飫镎故玖巳綰偽嘈碢HP程序實現驗證碼功能:
牐牬碼一:
/*
*?? Filename:??? authpage.php
*?? Author:?? hutuworm
*?? Date:?? 2003-04-28
*?? @Copyleft??? hutuworm.org
*/
srand((double)microtime()*1000000);
//驗證用戶輸入是否和驗證碼一致
if(isset($HTTP_POST_VARS['authinput']))
{
if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0)
echo "驗證成功!";
else
echo "驗證失敗!";
}
//生成新的四位整數驗證碼
while(($authnum=rand()%10000)<1000);
?>
請輸入驗證碼:
>
??????????????? >
牐牬碼二:
/*
*?? Filename:??? authimg.php
*?? Author:?? hutuworm
*?? Date:?? 2003-04-28
*?? @Copyleft??? hutuworm.org
*/
//生成驗證碼圖片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(58,28);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
//將四位整數驗證碼繪入圖片
imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);
for($i=0;$i<50;$i++)?? //加入干擾象素
{
imagesetpixel($im, rand()%70 , rand()%30 , $black);
}
ImagePNG($im);
ImageDestroy($im);
牐?>
牐牨疚某絳蛟贏pache 2.0.45 + PHP 4.3.1環(huán)境下運行通過。
上文只是對驗證碼功能的一個簡單實現,并沒有考慮商用安全性問題。如果要增強安全性,將此功能投入商業(yè)應用,則可以通過以下幾個步驟實現:
1. 啟用Session。
2. authnum在authimg.php中生成,并計算md5sum,存入session。
3. authpage.php將authinput計算md5sum后,與session中的authnum(md5sum)對比得出驗證結果。
本站注:作者使用了簡單的代碼實現了很酷的功能。不過在添加干擾像素時的效果不是太好,大家可以看一下雨聲論壇登錄時的效驗碼(http://ror.cn/perl/ut/user_login.cgi),偶把第二段代碼稍改了一下,生成了與其類似的效果。
修改后的代碼如下:
/*
*?? Filename: authimg.php
*?? Author:?? hutuworm
*?? Date:???? 2003-04-28
*?? @Copyleft hutuworm.org
*/
//生成驗證碼圖片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
while(($authnum=rand()%100000)<10000);
//將四位整數驗證碼繪入圖片
imagestring($im, 5, 10, 3, $authnum, $black);
for($i=0;$i<200;$i++)?? //加入干擾象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
?>
這就是微學網-程序員之家為你提供的"用PHP實現驗證碼功能"希望對你有所幫助.本文來自網絡,轉載請注明出處:http://www.weixuecn.cn/article/1578.html
總結
以上是生活随笔為你收集整理的php点击验证码,用PHP实现验证码功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: github java开源项目经验_3月
- 下一篇: 矿井通风计算c语言_矿井主通风机的技术发