<?php
/*
 *类名:验证码类
 *功能:通过该类的对象可以获取验证码图片,和验证码字符集($_SESSION['vericode'])
 *作者:小A、
 *时间:2012-7-3
 */
final class VeriCode
{
private $width; //验证码宽度
private $height;//验证码高度
private $codenum;//验证码个数
private $image;
private $randttf = false;//是否开启随机字体 true 为开启,默认关闭提供 4 种字体在 ttfs目录中
private $randcolor = true;//是否随机字体颜色 flase 为关闭 默认开启
private $bg = true;//背景底纹 function __construct($widht=60,$height=30,$codenum=4)
{
$this->widht = $widht;
$this->height = $height;
$this->codenum = $codenum;
}
function showcode()
{
$this->createcode();//创建画布
$this->createstring();//创建字符串
$this->createimage();//生成图像
}
private function createcode()//创建画布
{
$this->image = imagecreate($this->widht,$this->height);
$backcolor = imagecolorallocate($this->image,255,255,255);//如需改变背景色请设置这里的RGB
imagefill($this->image,0,0,$backcolor);
if($this->bg == true)
{
$bg = imagecolorallocate($this->image,221,221,221);
for($i = 0; $i < $this->widht / 2;$i++)//画竖线底纹
{
imageline($this->image,$i*2,0,$i*2,$this->height,$bg);
}
for($i = 0;$i < $this->height / 3;$i++)//画横向底纹
{
imageline($this->image,0,$i*3,$this->widht,$i*3,$bg);
}
}

}
private function createstring()//在画布写入字符串
{
$string = $this->codestring();
session_start();
$_SESSION["vericode"] = $string;
for($i = 0;$i < $this->codenum;$i++)
{
if($this->randttf == true)
{
$ttf = 'ttfs/t'.rand(1,9).'.ttf';//随机字体 请保证 ttfs文件夹 在同一目录里中,如改变路径请改变此路径
}else
{
$ttf = 'ttfs/t1.ttf';//9中字体请自己常识,只需要修改t4中数字 1 - 9看看到每种字体的效果,选择自己喜欢的 注:请关掉随机字体测试
}
$fontsize = rand(14,15);//产生随机字体大小
$fontangle = rand(-10,10);//字符倾斜角度  随即倾斜
$x = $i*12+4; 
$y =rand($fontsize,$this->height-8);
if($this->randcolor == true)
{
$textcolor = imagecolorallocate($this->image,rand(0,180),rand(0,180),rand(0,180));
}else
{
$textcolor = imagecolorallocate($this->image,0,0,0);
}
imagettftext($this->image,$fontsize,$fontangle,$x,$y,$textcolor,$ttf,$string[$i]);
}
}
private function codestring()//生成随机字符串
{
$string = '';
for($i=0;$i < $this->codenum;$i++)
{
$num = rand(1,1);
/*
 * 如果想是单一的格式,请参考:
 *
 * 只要需要数字
 *
 * 把上边的 $num = rand(1,3) 改成 $num = rand(2,2)
 *
 * 只需要小写字母
 *
 * 把上边的 $num = rand(1,3) 改成 $num = rand(3,3)
 *
 * 只需要大写字母
 *
 * 把上边的 $num = rand(1,3) 改成 $num = rand(1,1)
 *
 * 只需要 小写字母 和 数字
 *
 * 把上边的 $num = rand(1,3) 改成 $num = rand(3,2)
 *
 */
switch($num)
{
case 1:
$num2 = rand(65,90); //随机产生小写字母 a - z 所对应的ASCII码的值
break;
case 2:
$num2 = rand(51,57); //随机产生数字 2 9 所对应的ASCII码的值 如果是rand(48,57):0-9将有0 这样不利于用户判断
break;
case 3:
$num2 = rand(97,122); //随机产生大写字母 A - Z 所对应的ASCII码的值
break;
} /*
 *为用用户着想去除了 0 o I i 1 z Z 2
 *
 */ if($num2 == 111 || $num == 105 || $num == 122 || $num2 == 79 || $num2 == 73 || $num == 90)//如果是大写字母中的O,I用P代替
{
$num2 = 112; }
$tmp = sprintf("%c",$num2); //用sprintf函数来得到产生ASCII码所对应的字符
$string .=$tmp;
}
return $string;
}
private function createimage()//生成图像
{
if(function_exists("imagegif"))
{
header("Content-type:image/gif");
imagegif($this->image);
}elseif(function_exists("imagejpeg"))
{
header("Content-type:image/jpeg");
imagejpeg($this->image,"",50);//50为图像的品质,0-100 0质量最差,图像文件越小 100质量最好,图像文件越大
}elseif(function_exists("imagepng"))
{
header("Content-type:image/png");
imagepng($this->image);
}elseif(function_exists("imagewbmp"))
{
header("Content-type:image/vnd.wap.wbmp");
imagewbmp($this->image);
}else
{
die('服务器不支持图像,请检查GD库');
}
} function __destruct()
{
imagedestroy($this->image);

}
}
?>上边的是验证码类下边是用来测试页面<?php
session_start();
function __autoload($filename)
{
require  $filename.'.class.php';
}
$code = new VeriCode;
$code->showcode();
if(strtoupper($_GET["code"]) != $_SESSION["vericode"] )
{
echo  '验证码输入错误';
}else
{
echo '验证码正确';
}?>
我在类中已经讲 验证码字符串赋值给了 $_SESSION['vericode'] 但是我在页面测试的时候获取不到 打印session数组也无结果   求解  

解决方案 »

  1.   


    没有二级域名,  我大概说下,我是在  验证码类中直接赋值给$_SESSION的,但是在其他页面的我得不到我在类里边赋值的SESSION
      

  2.   

    没有二级域名, 我大概说下,我是在 验证码类中直接赋值给$_SESSION的,但是在其他页面的我得不到我在类里边赋值的SESSION
      

  3.   

    其他页面 session_start(); 了吗?
      

  4.   

    没初始化类的缘故吧
    function __construct($widht=60,$height=30,$codenum=4)
      

  5.   

    php简单验证码类(字母+数字)
    http://3aj.cn/php/27.html