+ -
当前位置:首页 → 问答吧 → 一个验证码类及用法

一个验证码类及用法

时间:2009-08-23

来源:互联网

类文件:ZC_Verify.php

[php]
<?php
//图片验证码类
//www.zzcms.com

class verify{
    private $im;//图像资源
    private $width;//图片宽度
    private $height;//图片高度
    private $bgcolor;//背景颜色
    private $point_num;//在图像上所画点的个数
    private $point_color;//干扰像素颜色
    private $line_num;//在图像上所画线的条数
    private $line_color;//干扰线条颜色
    private $font_num;//验证字符个数
    
    function __construct(){
    }
    
    //设定图像大小
    function set_size(){
        if(empty($this->font_num)){
            $this->font_num=4;
        }
        $this->width=$this->font_num*12+4;// 根据字符计算图像宽度
        $this->height=20;
    }
    
    //建立图像
    function create_pic(){
        $this->im=imagecreate($this->width,$this->height);
        imagecolorallocate($this->im,200,200,200);
    }
    
    //设置干扰点
    function set_point(){
        if(empty($this->point_num)){
            $this->point_num=50;
        }
        for($i=0;$i<$this->point_num;$i++){
            $this->point_color=imagecolorallocate($this->im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//生成干扰像素颜色
            imagesetpixel($this->im,$this->width,$this->height,$this->point_color);//生成干扰像素
        }
    }
        
    //设置干扰线条
    function set_line(){
        if(empty($this->line_num)){
            $this->line_num=2;
        }
        for($i=0;$i<$this->line_num;$i++){
            $this->line_color=imagecolorallocate($this->im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//生成干扰线条颜色
            imageline($this->im,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$this->line_color);
        }
    }
    
    //生成随机字符,并且MD5加密
    function ver_str(){
        $string=strtoupper(md5(mt_rand(0,8)));// 用MD5加密随机产生的数字
        if(empty($this->font_num)){
            $this->font_num=4;
        }
        return substr($string,0,$this->font_num);
    }
    
    
    //写入验证字符
    function show(){
        $this->set_size();//设置图像大小
        $this->create_pic();//建立图像
        $string=$this->ver_str();//得到随时字符
        for($i=0;$i<$this->font_num;$i++){
            $font_color=imagecolorallocate($this->im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
            imagestring($this->im,5,$i*10+8,mt_rand(1,7),$string[$i],$font_color);
        }
        $this->set_point();//绘制干扰点
        $this->set_line();//绘制干扰线
        //header("Contetn-type:image/png");
        imagepng($this->im);//输出图像
        imagedestroy($this->im);//释放内存
        return $string;
    }        
}

?>
[/php]

生成验证码文件:ver.php

[php]
<?php
    session_start();
    header("Content-type: image/png");
    include("../lib/ZC_Verify.php");
    $image=new verify();
    $_SESSION['ver_code']=$image->show();
?>
[/php]

调用实例
[php]
<img src="../include/ver.php" onclick="this.src='../include/ver.php?id='+Math.random()*5;" alt="验证码,看不清楚?请点击刷新验证码" />
[/php]


如有BUG,请到及时通知我,谢谢

作者: byyy521   发布时间: 2009-08-23

学习了

作者: pandao   发布时间: 2009-08-24

直接用啦

作者: sdlywwb   发布时间: 2009-08-24

不错,拿去直接用了。

作者: lifeman   发布时间: 2009-08-25

好像有问题,图像输出没有任何反应。是不是需要配置下PHP的GD库 
测试环境:WINDOWS下PHP4.4.0

作者: lifeman   发布时间: 2009-08-26

引用
引用第4楼lifeman于2009-08-26 10:23发表的  :
好像有问题,图像输出没有任何反应。是不是需要配置下PHP的GD库 
测试环境:WINDOWS下PHP4.4.0


应该是你的环境问题

作者: byyy521   发布时间: 2009-08-31

测试了下.不错.

作者: memory   发布时间: 2009-09-18

作者: philips35   发布时间: 2009-09-19

学习了

作者: yuhuaan   发布时间: 2009-09-21

学习了。

作者: yuhuaan   发布时间: 2009-09-21

先用看看

作者: 84uu   发布时间: 2009-10-17

不错 学习了

作者: wxl1916   发布时间: 2009-10-19

用什么来进行判断?他输出数值的变量是什么?

作者: nikai   发布时间: 2009-10-25

不知道怎么用啊?

作者: a604b   发布时间: 2009-10-28

  

作者: ln850522   发布时间: 2009-12-15

学习了。

作者: gww   发布时间: 2009-12-21

支持一下了。

作者: bingshao   发布时间: 2009-12-21