未命名  

不少網站為了防止用戶利用機器人自動註冊、登錄、灌水,都採用了驗證碼技術。所謂驗證碼,就是將一串隨機產生的數字或符號,生成一幅圖片,圖片裡加上一些干擾象素(防止OCR),由用戶肉眼識別其中的驗證碼信息,輸入表單提交網站驗證,驗證成功後才能使用某項功能。 

我們這裡展示了如何編寫PHP程序實現驗證碼功能: 

代碼一:

 

    <?php 
   /* 
    *   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);  
    ?> 
        <form action=authpage.php method=post> 
        <table> 
                請輸入驗證碼:<input type=text name=authinput style="width: 80px"><br> 
                <input type=submit name="驗證" value="提交驗證碼"> 
                <input type=hidden name=authnum value=<? echo $authnum; ?>> 
                <img src=authimg.php?authnum=<? echo $authnum; ?>> 
        </table> 
        </form>

 代碼二: 

 
<?php
   /* 
    *   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); 
?>



本文程序在Apache 2.0.45 + PHP 4.3.1環境下運行通過。

上文只是對驗證碼功能的一個簡單實現,並沒有考慮商用安全性問題。如果要增強安全性,將此功能投入商業應用,則可以通過以下幾個步驟實現: 

1. 啟用Session。 
2. authnum在authimg.php中生成,並計算md5sum,存入session。 
3. authpage.php將authinput計算md5sum後,與session中的authnum(md5sum)對比得出驗證結果。



本站註:作者使用了簡單的代碼實現了很酷的功能。不過在添加干擾像素時的效果不是太好,大家可以看一下雨聲論壇登錄時的效驗碼(http://ror.cn/perl/ut/user_login.cgi),偶把第二段代碼稍改了一下,生成了與其類似的效果。

修改後的代碼如下:

<?php 
/* 
 *   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); 
?>

 

檔案下載:34682273

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()