PHP認証コードアプリケーション、例


最近PHPで認証コードプログラムを作ってみました.例は以下の通りです.
 
一、検証コードを表示して提出するページを用意する
<?php

    @header("content-type:text/html; charset=UTF-8");

    //  session

    session_start();

?>

<html>

    <head>

       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

       <title>PHP     </title>

    </head>

    <body>

          :<br/>

       <iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" ></iframe>

       <br/>

       <input type=button value="   ,   " onclick="iimg.location.reload();">

       <br>

       <form action="validate.php" method="post">

                :<input name="imgId" style="width:60">

           <input type="submit" value="  ">

       </form>

    </body>

</html>

 
二、以下は認証コード生成ページであり、このページは第1ページでに呼び出される
<?php

Header("Content-type: image/gif");

session_start();

//        ,     

$randval;

for($i=0;$i<7;$i++){

    $randstr = mt_rand(ord('A'),ord('Z'));

    srand((double)microtime()*1000000);

    $randv = mt_rand(0,10);

    if($randv%2==0){

       $randval.=mt_rand(0,10);

    }else{

       $randval.=chr($randstr);

    }

}

//      session

session_register($randval);

//         

$height = 50;//  

$width = 100;//  

$im = ImageCreateTrueColor($width, $height);

$white = ImageColorAllocate($im, 255, 255, 255);

$blue = ImageColorAllocate($im, 0, 0, 64);

ImageFill($im, 0, 0, $white);

srand((double)microtime()*1000000000);

ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue);

srand((double)microtime()*1000000);

ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue);

srand((double)microtime()*1000000);

ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);

ImageGIF($im);

ImageDestroy($im);

/*

      :          ,     PHP  GD2     ,    php.ini     

;extension=php_gd2.dll

 

extension=php_gd2.dll

   GD2 

*/

?>

 
三、これは検証ページで、検証に合格したかどうかを示す
<?php @header("content-type:text/html; charset=UTF-8");

    //  session

    session_start();

    //          ,      

    $imgId_req = $_REQUEST['imgId'];

    $imgId_req = strtoupper($imgId_req);

    //           session  

    if (session_is_registered($imgId_req)) {

       echo "<font color=blue >    !</font>";

    } else {

       echo "<font color=red >    !</font>";

    }

    //  session,           

    session_destroy();

?>

 
四、サービスを開き、アドレスバーに「.../input.php」と入力し、ここではホストとディレクトリ名を省略する
 
五、GD 2ライブラリの詳細については、以下を参照してください.
http://www.cndw.com/tech/server/2006042051613.asp