全省快速服務,舊屋翻修、廠房拆除等大小工程
價格透明線上可查,事後清運還原
台北居家清潔推薦NO1搬家公司,免費紙箱,服務不打烊
打包家當不煩惱,落地不加價,歡迎來電預約估價

首頁  •  j2h 論壇 • 程式設計討論     • 

php 的密碼驗證 範例

房東:達人
發表時間:2007-01-18


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

??我們這裡展示了如何編寫PHP程序實現驗證碼弁遄G

??代碼一:


[Copy to clipboard]CODE:
/*
* 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 "驗證成央I";
else
echo "驗證失敗!";
}

//生成新的四位整數驗證碼
while(($authnum=rand()%10000)<1000);
?>


請輸入驗證碼:


>
>


?>


代碼二:


[Copy to clipboard]CODE:
/*
* 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環境下運行通過。

上文只是對驗證碼弁鄋漱@個簡單實現,並沒有考慮商用安全性問題。如果要增強安全性,將此弁鄑諵J商業應用,則可以通過以下幾個步驟實現:

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


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

修改後的代碼如下:


[Copy to clipboard]CODE:
/*
* 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);
?>

會出現一個 圖片要求再輸入一次 四碼的數字 再驗證~





  • 贊助網站       

    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!
    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務

  • 1 樓住戶:小優
    發表時間:2007-03-22

    在php中動態生成不同字體顏色的png格式驗証圖片
    session_start();
    //生成驗証碼圖片
    Header("Content-type: image/PNG");
    $authnum=$_SESSION["authnum"];
    srand((double)microtime()*1000000);
    $len = strlen($authnum)*12+10;
    $im = imagecreate($len,25);
    $color[]= array ();
    $color[1] = ImageColorAllocate($im, 0x00,0x00,0x00);
    $color[2] = ImageColorAllocate($im, 0x00,0x00,0xff);
    $color[3] = ImageColorAllocate($im, 0xff,0x33,0x00);
    $color[4] = ImageColorAllocate($im, 0x00,0x00,0x99);
    $color[5] = ImageColorAllocate($im, 0xff,0x00,0xff);
    $color[6] = ImageColorAllocate($im, 0x99,0x66,0xff);
    $color[7] = ImageColorAllocate($im, 0x00,0x99,0x99);
    $color[8] = ImageColorAllocate($im, 0xff,0xff,0x00);

    if($background)
    {
    $r = substr($background, 0, 2);
    $g = substr($background, 2, 2);
    $b = substr($background, 4, 2);
    $bg = ImageColorAllocate($im, hexdec("0x".$r),hexdec("0x".$g),hexdec("0x".$b));
    }
    else
    {
    $bg = ImageColorAllocate($im, 0xcc,0xcc,0xff);
    }
    imagefill($im, 0, 0, $bg);

    for($i=0,$x=5;$i {
    imagestring($im, 5, $x, rand(2,8), $authnum[$i], $color[rand(1,8)]);
    $x+=12;
    }
    for($i=0;$i<100;$i++) //加入干擾象素
    {
    $randcolor = ImageColorallocate($im,rand(100,255),rand(100,255),rand(100,255));
    imagesetpixel($im, rand()%$len , rand()%30 , $randcolor);
    }
    ImagePNG($im);
    ImageDestroy($im);
    ?>





    2 樓住戶:陌生人
    發表時間:2007-04-10

    PHP生成帶有雪花背景的驗証碼






    //檢驗校驗碼
    if(isset($HTTP_POST_VARS["sub"])):
    if($HTTP_POST_VARS["number"] != $HTTP_SESSION_VARS[login_check_number] || empty($HTTP_POST_VARS["number"])){
    echo "校驗碼不正確!" ;
    }else{
    echo"驗証碼透過﹗";
    }
    endif;
    show_source('test.php');
    //以上本頁的源碼


    //以下是生成驗証碼的源碼
    show_source('YanZhengMa.php');
    ?>
    session_start();
    session_register("login_check_number");
    //昨晚看到了chianren上的驗証碼效果,就考慮了一下,用PHP的GD庫完成了類似功能
    //先成生背景,再把生成的驗証碼放上去
    $img_height=120; //先定義圖片的長、寬
    $img_width=40;
    if($HTTP_GET_VARS["act"]== "init"){
    //srand(microtime() * 100000);//PHP420後,srand不是必須的
    for($Tmpa=0;$Tmpa<4;$Tmpa++){
    $nmsg.=dechex(rand(0,15));
    }//by sports98


    $HTTP_SESSION_VARS[login_check_number] = $nmsg;

    //$HTTP_SESSION_VARS[login_check_number] = strval(mt_rand("1111","9999")); //生成4位的隨機數,放入session中
    //誰能做下補充,可以同時生成字母和數字啊??----由sports98完成了

    $aimg = imageCreate($img_height,$img_width); //生成圖片
    ImageColorAllocate($aimg, 255,255,255); //圖片底色,ImageColorAllocate第1次定義顏色PHP就認為是底色了
    $black = ImageColorAllocate($aimg, 0,0,0); //定義需要的黑色
    ImageRectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//先成一黑色的矩形把圖片包圍

    //下面該生成雪花背景了,其實就是在圖片上生成一些符號
    for ($i=1; $i<=100; $i++) { //先用100個做測試
    imageString($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"*",imageColorAllocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
    //哈,看到了吧,其實也不是雪花,就是生成﹡號而已。為了使它們看起來"雜亂無章、5顏6色",就得在1個1個生成它們的時候,讓它們的位置、顏色,甚至大小都用隨機數,rand()或mt_rand都可以完成。
    }

    //上面生成了背景,現下就該把已經生成的隨機數放上來了。道理和上面差不多,隨機數1個1個地放,同時讓他們的位置、大小、顏色都用成隨機數~~
    //為了區別於背景,這裡的顏色不超過200,上面的不小於200
    for ($i=0;$i imageString($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(1,10),mt_rand(1,$img_width/2), $HTTP_SESSION_VARS[login_check_number][$i],imageColorAllocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
    }
    Header("Content-type: image/png"); //告訴瀏覽器,下面的數據是圖片,而不要按文字顯示
    ImagePng($aimg); //生成png格式。。。嘿嘿效果蠻像回事的嘛。。。
    ImageDestroy($aimg);
    }

    ?>




    3 樓住戶:阿龍
    發表時間:2007-04-10



    3.validate_user.php
    作用︰驗証用戶登錄

    部分關鍵源代碼︰
    session_start();
    if (strtoupper($HTTP_POST_VARS[’validate_code’]) != $_SESSION[’validate_code’])
    {
    echo ’’;
    unset($_SESSION[’admin_type’]);
    unset($_SESSION[’validate_code’]);
    exit;
    }
    else
    驗証用戶名密碼是否匹配
    ?>

    header ("Content-type: image/png");
    session_start();
    $_SESSION[’validate_code’] = strtoupper(substr(md5(rand()),20,6));
    $im = @imagecreate (130, 40)
    or die ("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate ($im, 200, 200, 200);

    //設定干擾像素,防止被OCR
    for ($i=0;$i<=128;$i++)
    {
    $point_color = imagecolorallocate ($im, rand(0,255), rand(0,255), rand(0,255));
    imagesetpixel($im,rand(2,128),rand(2,38),$point_color);
    }

    //逐個畫上驗証碼字符
    for ($i=0;$i<=5;$i++)
    {
    $text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));
    $x = 10 + $i * 20;
    $y = rand(5,20);
    imagechar ($im, 5, $x, $y, $_SESSION[’validate_code’]{$i}, $text_color);
    }

    //輸出PNG圖像
    imagepng ($im);
    imagedestroy ($im);
    ?>

    2.login.php
    作用︰生成登錄界面,顯示驗証碼圖片
    部分關鍵代碼(不是為了守密,是HTML代碼太多,無助於理解程式,所以省省篇幅)
    ...
    session_start();
    ...
    ?>
    驗証碼︰
    (0-9,A-F共16個字符,沒有字母o只有數字0)

    4 樓住戶:驕子
    發表時間:2007-04-12


    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(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);
    ?>



     共 4 人回應  選擇頁數 【第1 頁】 

    姓名:
    佈告內容:
    其他選項: