PHP--验证码类及具体使用方法

原文来自:http://www.oschina.net/code/snippet_106025_6280

验证码类

Captcha.class.php

<?php
/**
 * 验证码类
 * 
 * 调用方法:
 * require_once 'Captcha.class.php';
 * $captcha = new Captcha(80,30,4);
 * $captcha->showImg();
 * 
 * Enter description here ...
 * @author Yahoo
 *
 */
class Captcha
{
	//VAR
    private $width;
    private $height;
    private $codeNum;	//验证码长度
    private $code;		//验证码内容
    private $im;		//输出验证码图片

    //Construct
    function __construct($width=80, $height=20, $codeNum=4)
    {
        $this->width = $width;
        $this->height = $height;
        $this->codeNum = $codeNum;
    }

    //Methods
    function showImg()
    {
        //创建图片
        $this->createImg();
        //设置干扰元素
        $this->setDisturb();
        //设置验证码
        $this->setCaptcha();
        //输出图片
        $this->outputImg();
    }

    function getCaptcha()
    {
        return $this->code;
    }

    private function createImg()
    {
        $this->im = imagecreatetruecolor($this->width, $this->height);
        $bgColor = imagecolorallocate($this->im, 0, 0, 0);
        imagefill($this->im, 0, 0, $bgColor);
    }

    private function setDisturb()
    {
        $area = ($this->width * $this->height) / 20;
        $disturbNum = ($area > 250) ? 250 : $area;
        //加入点干扰
        for ($i = 0; $i < $disturbNum; $i++) {
            $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
        }
        //加入弧线
        for ($i = 0; $i <= 5; $i++) {
            $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
            imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
        }
    }

    private function createCode()
    {
        $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";

        for ($i = 0; $i < $this->codeNum; $i++) {
            $this->code .= $str{rand(0, strlen($str) - 1)};
        }
    }

    private function setCaptcha()
    {
        $this->createCode();

        for ($i = 0; $i < $this->codeNum; $i++) {
            $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
            $size = rand(floor($this->height / 5), floor($this->height / 3));
            $x = floor($this->width / $this->codeNum) * $i + 5;
            $y = rand(0, $this->height - 20);
            imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
        }
    }

    private function outputImg()
    {
        if (imagetypes() & IMG_JPG) {
            header('Content-type:image/jpeg');
            imagejpeg($this->im);
        } elseif (imagetypes() & IMG_GIF) {
            header('Content-type: image/gif');
            imagegif($this->im);
        } elseif (imagetypes() & IMG_PNG) {
            header('Content-type: image/png');
            imagepng($this->im);
        } else {
            die("Don't support image type!");
        }
    }

}

具体使用如下:

(1) 登录界面截取部分:

	<tr>
		<td>验证码:</td>
		<td>
			<input type="text" name="checkcode" />
		</td>
		<td>
			<img alt="点击变换" src="tools/checkCode.php" 
				οnclick="this.src='tools/checkCode.php?aa='+Math.random()"
			/>
		</td>
	</tr>


(2) 验证码处理模块:

checkCode.php

<?php
	require_once 'Captcha.class.php';
	//创建验证码实例
	$captcha = new Captcha(80, 30, 4);
	//产生验证码图片
	$captcha->showImg();
	//把产生的验证码保存到sesssion中
	$code = $captcha->getCaptcha();
	session_start();
	$_SESSION['checkcode'] = $code;
?>

(3) 登录验证码处理模块截取:

			//检查验证码是否正确
			if (!empty($_POST['checkcode'])){
				session_start();
				$checkCode = $_SESSION['checkcode'];
				if ($checkCode != $_POST['checkcode']){
					header("Location: login.php?errorno=3");
					exit();
				}
			}else{
				header("Location: login.php?errorno=3");
				exit();				
			}

大功告成!


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值