以下验证码生成的类,通过一下代码可以在网页显示验证码
<img src="dnsing_authcode.class.php" border="0" id="authcode">现在本人问题为:
当客户端输入验证码并提交时,如何判断输入的验证码跟产生的验证码是否正确?请附上简单代码,谢谢

类文件名:dnsing_authcode.class.php 以下是代码片段: 
<?php 
/* 
+---------------------------------------------------+ 
| Name : DNSingImageCodeClass (生成随机验证码图片类) 
+---------------------------------------------------+ 
| Created / Modify : 2006-2-10 6:59 / -- -- 
+---------------------------------------------------+ 
| Version : 0.0.1 
+---------------------------------------------------+ 
| Author : Edison Cai 
+---------------------------------------------------+ 
| Powered by DNSing Network 
+---------------------------------------------------+ 
| Email : [email protected] 
| Homepge : http://www.dnsing.com 

+---------------------------------------------------+ 
*/ 
Class DNSingImageCode 

/* 
+---------------------------------------------------+ 
| 配置文件 
+---------------------------------------------------+ 
*/ 
var $x = 10; // 字符左间距 
var $y = 2; // 字符顶端距 
var $width = 60; // 生成图片的宽度 
var $height = 20; // 生成图片的高度 
var $length = 4; // 验证码长度 
var $fontStyle = 5; // 字体样式 ( 1-5 ) 
var $fontSpace = 10; // 字符与字符间间距 
var $sessionName = 'DNSing_AUTH_STRING'; // SESSION 变量名,不能为空,可以任意设置 
var $stringRange = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // 验证码范围 
var $imagePixel = true; // 是否生成干扰像素 
var $imageBorder = true; // 是否生成图片边框 
/* 
+---------------------------------------------------+ 
| 颜色 
+---------------------------------------------------+ 
| 格式 
+---------------------------------------------------+ 
| 颜色名称 => 颜色代码 
+---------------------------------------------------+ 
*/ 
var $imageColor = array( 
'silver' => 'F7F7F7', // 银色 
'black' => '000000', // 黑色 
'blue' => '0000FF', // 蓝色 
'white' => 'FFFFFF', // 白色 
'red' => 'FF0000', // 红色 
); 
var $defaultStyle = 2; // 默认样式 
/* 
+---------------------------------------------------+ 
| 样式 
+---------------------------------------------------+ 
| 格式 
+---------------------------------------------------+ 
| 1 => 字体颜色 
| 2 => 边框颜色 
| 3 => 背景颜色 
+---------------------------------------------------+ 
*/ 
var $colorStyle = array( 
1 => array( 
1 => 'black', 
2 => 'black', 
3 => 'white' 
), 
2 => array( 
1 => 'blue', 
2 => 'black', 
3 => 'silver' 
), 
3 => array( 
1 => 'white', 
2 => 'red', 
3 => 'black' 
), 
); 
//+---------------------------------------------------+ 
var $imageColors = array(); 
var $backroundColor; 
var $borderColor; 
var $fontColor; 
function DNSingImageCode() 

$this->setRandomSeek(); 
$this->setCodeString(); 
$this->setSession(); 
$this->initializationImage(); 
$this->setImageColor(); 
$this->setColorStyle($this->defaultStyle); 
$this->setImageBackground(); 
$this->setImageString(); 
$this->setImageBorder(); 
$this->setImagePixel(); 
$this->outputImage(); 

function setCodeString() 

for ($i = 0; $i < $this->length; $i++) 

$seek = rand(0,strlen($this->stringRange) - 1); 
$this->codeString .= $this->stringRange[$seek]; 


function setRandomSeek() 

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

function setSession() 

session_start(); 
session_register($this->sessionName); $_SESSION[$this->sessionName] = $this->codeString; 

function initializationImage() 

$this->DNSingImage = imagecreate($this->width, $this->height); 

function setImageColor() 

foreach($this->imageColor as $key => $value) 

$this->imageColors[$key] = imagecolorallocate($this->DNSingImage, hexdec(substr($value, 0, 2)), hexdec(substr($value, 2, 2)), hexdec(substr($value, 4, 2))); 


function _setFontColor($color) 

$this->fontColor = $this->imageColors[$color]; 

function _setBorderColor($color) 

$this->borderColor = $this->imageColors[$color]; 

function _setBackgroundColor($color) 

$this->backroundColor = $this->imageColors[$color]; 

function setColorStyle($s) 

$this->_setFontColor($this->colorStyle[$s][1]); 
$this->_setBorderColor($this->colorStyle[$s][2]); 
$this->_setBackgroundColor($this->colorStyle[$s][3]); 

function setImageBackground() 

imagefill($this->DNSingImage, 0, 0, $this->backroundColor); 

function setImageString() 

for ($i = 0; $i < strlen($this->codeString); $i++) 

imagestring($this->DNSingImage, $this->fontStyle, $this->fontSpace * $i + $this->x, $this->y, substr($this->codeString, $i, 1), $this->fontColor); 


function setImageBorder() 

if ($this->imageBorder) 

imageline($this->DNSingImage, 0, 0, 0, $this->height, $this->borderColor); 
imageline($this->DNSingImage, 0, 0, $this->width - 1, 0, $this->borderColor); 
imageline($this->DNSingImage, $this->width - 1, 0, $this->width - 1, $this->height, $this->borderColor); 
imageline($this->DNSingImage, 0, $this->height - 1, $this->width - 1, $this->height - 1, $this->borderColor); 


function setImagePixel() 

if ($this->imagePixel) 

$pixelNum = ($this->width * $this->height) / 20; 
for($i = 0; $i < $pixelNum; $i++) 

$randColor = ImageColorAllocate($this->DNSingImage, rand(0, 250), rand(0, 250), rand(0, 250)); 
imagesetpixel($this->DNSingImage, rand(0, $this->width), rand(0, $this->height), $randColor); 



function outputImage() 

header("Content-type: image/PNG"); 
ImagePNG($this->DNSingImage); 
ImageDestroy($this->DNSingImage); 


$DNSingIC = new DNSingImageCode(); 
?> 

解决方案 »

  1.   

    类里不是注明了session的变量名不能为空么?
    var $sessionName = 'DNSing_AUTH_STRING'; // SESSION 变量名,不能为空,可以任意设置你验证提交的验证码与这个session是不是一致就可以了。如:
    if($_SESSION['DNSing_AUTH_STRING'] == $_POST['验证框name']){}
      

  2.   

    你上面的代码应该把生成的验证码存入session里面,然后post的时候与这个session判断就行了。
      

  3.   


    我用echo 显示$_SESSION['DNSing_AUTH_STRING'],结果为空,是不是我的类有问题,还是我调用出错了?
    由于我对PHP类使用不熟悉,请写出简要代码,谢谢
      

  4.   


    上面的类代码不是已经把验证码存入session里了吗?