PHPランダムコード画像の生成と検証


一般的な方法:
checkNumbers.php
<?php 
session_start();
if($act  ==  "init") 
{ 
        header("content-type:  image/png"); 
        //srand(microtime()  *  100000);		
        //$login_check_number  =  strval(rand("1111","9999")); 
		$login_check_number  =  generate_password( 4 );
        session_register("login_check_number"); 
        //      session      . 
        //      cookie 
        //setcookie("login_check_number",$login_check_number); 
        //       session_start()  ; 
        //     cookie,    cookie          . 
        $h_img  =  imagecreate(40,17); 
        $c_black  =  imagecolorallocate($h_img,  0,0,0); 
        $c_white  =  imagecolorallocate($h_img,  255,255,255); 
        imageline($h_img,  1,  1,  350,  25,  $c_black); 
        imagearc($h_img,  200,  15,  20,  20,  35,  190,  $c_white); 
        imagestring($h_img,  5,  2,  1,  $login_check_number,  $c_white); 
        imagepng($h_img); 
        imagedestroy($h_img); 
        die(); 
}
function generate_password( $length = 4 ) {  
	//      ,             
	$chars = '0123456789';   
	//$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	  
	$password = '';  
	for ( $i = 0; $i < $length; $i++ )   
	{  
	//               
	//        substr   $chars        ;  
	//           $chars        
	// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);  
	$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];  
	}  
	return $password;  
}   
?>

ランダムコード生成コードを呼び出すログインページ:login.php
<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>    </title>
</head>
<?
include_once('./checkNumbers.php'); 
if(isset($_POST['number']))
{
	$number = $_POST['number'];
	
	echo $number.' + ';
	
	if($number != $login_check_number )
	{
		print("      !");
	
		die();
	}
	else
		printf("Successful.");
	
}
?>
<?php 
//    : 
// HTML          PHP         (  :           ,     SESSION) 
//$number              
//        
?>
<body>
<form method="post">
<input  type="text"  name= "number"  maxlength = "4"><img  src=checkNumbers.php?act=init>
<input type="submit" name="submit" />
</form>
</body>
</html>

二検証コード生成クラス
<?php  
class ValidationCode{  
        private $width;  
        private $height;  
        private $codeNum;  
        private $image;  
        private $disturbColorNum;  
        private $checkCode;  
          
        function __construct($width=80,$height=40,$codeNum=4){  
            $this->width=$width;  
            $this->height=$height;  
            $this->codeNum=$codeNum;  
            $this->checkCode=$this->createCheckCode();  
            $number=floor($width*$height/15);  
            if($number>240-$codeNum){  
            $this->disturbColorNum=240-$codeNum;  
            }  
            else{  
            $this->disturbColorNum=$codeNum;  
            }  
              
        }  
        //                 
        function showImage(){  
            //        
            $this->createImage();  
            //        
            $this->setDisturbColor();  
            //      
            $this->outputText();  
            //      
            $this->outputImage();  
        }     
          
        //                      
        function getCheckCode(){  
            return $this->checkCode;  
        }  
        //        
        private function createImage(){  
            //        
            $this->image=imagecreatetruecolor($this->width,$this->height);  
            //         
            $backColor=imagecolorallocate($this->image,rand(100,255),rand(120,255),rand(120,255));  
            //       
            imagefill($this->image,0,0,$backColor);  
            //        
            $border=imagecolorallocate($this->image,0,0,0);  
            //        
            imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);  
        }  
        //        
        private function setDisturbColor(){  
            //       
            for($i=0;$i<$this->disturbColorNum;$i++){  
            $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));  
            imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);  
            }  
            //      ,  10   
            for($i=0;$i<10;$i++){  
            //      
            $color=imagecolorallocate($this->image,rand(2,255),rand(2,244),rand(2,244));  
            imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(20,200),rand(30,300),39,34,$color);  
            }  
        }  
        //        
        private function createCheckCode(){  
            $code="123456789QWERTYUIPLKJHGFDSAZXCVBNM";  
            $string='';  
            for($i=0;$i<$this->codeNum;$i++){  
                $char=$code{rand(0,strlen($code)-1)};  
                $string.=$char;  
            }  
            return $string;  
           
        }     
        //         
        private function outputText(){  
            for($i=0;$i<$this->codeNum;$i++){  
                $fontcolor=imagecolorallocate($this->image,rand(0,125),rand(1,124),rand(2,125));  
                $fontsize=rand(2,5);  
                $x=floor($this->width/$this->codeNum)*$i+3;  
                $y=rand(0,$this->height-15);  
                imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor);  
            }  
        }  
          
        private function outputImage(){  
        if(imagetypes()&IMG_GIF){  
            header("Content-Type:image/gif");  
            imagepng($this->image);  
        }  
        else if(imagetypes()&IMG_JPG){  
            header("Content-Type:image/jpeg");  
            imagepng($this->image);  
        }  
        else if(imagetypes()&IMG_PNG){  
            header("Content-Type:image/png");  
            imagepng($this->image);  
        }         
        }  
          
        function __destruct(){            
            imagedestroy($this->image);        
            }         
} 
?>