用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

验证码生成类php

2013-10-15 作者: 免费源代码下载整理举报

[php]代码库

<?php
/*
	more & original php framwork
	copyright (c) 2007 - 2008 ismole inc.

	$id: mooseccode.class.php 38 2008-03-19 07:39:17z aming $
*/

!defined('IN_MOOPHP') && exit('Access Denied');

class MooSeccode {
	//note:生成的验证码
	var $cecCode = '';
	//note:生成的图片
	var $codeImage = '';
	//note:干扰素
	var $disturColor = '';
	//note:验证码的图片宽度
	var $codeImageWidth = '80';
	//note:验证码的图片高度
	var $codeImageHeight  = '20';
	//note:验证码位数
	var $cecCodeNum = 4;
	
	/**
	 * 输出头部
	 *
	 */
	function outHeader() {
		header("content-type: image/png");
	}
	
	/**
	 * 生成验证码
	 *
	 */
	function createCode() {
		$this->cecCode = strtoupper(substr(md5(rand()),0,$this->cecCodeNum));
		return $this->cecCode;
	}

	/**
	 * 生成验证码图片
	 *
	 */
	function createImage() {
		$this->codeImage = @imagecreate($this->codeImageWidth,$this->codeImageHeight);
		imagecolorallocate($this->codeImage, 200, 200, 200);
		return $this->codeImage;
	}
	
	/**
	 * 加入图片干拢素
	 *
	 */
	function setDisturbColor() {
		for ($i=0; $i<=128; $i++) {
			$this->disturColor = imagecolorallocate($this->codeImage, rand(0,255), rand(0,255), rand(0,255));
			imagesetpixel($this->codeImage,rand(2,128),rand(2,38),$this->disturColor);
		}
	}

	/**
	 * 设置验证码图片的大小
	 *
	 * @param integer $width:
	 * @param integer $height:
	 * @return boolean;
	 */
	function setCodeImage($width, $height) {
		if($width == '' || $height == '') { return false; }
		$this->codeImageWidth = $width;
		$this->codeImageHeight = $height;
		return true;
	}

	/**
	 * 在图片上写入验证码
	 *
	 * @param integer $num
	 */
	function writeCodeToImage($num = '') {
		if($num != '') {$this->cecCodeNum = $num;}
		for($i = 0; $i <= $this->cecCodeNum; $i++) {
			$bg_color = imagecolorallocate ($this->codeImage, rand(0,255), rand(0,128), rand(0,255));
			$x = floor($this->codeImageWidth / $this->cecCodeNum) * $i;
			$y = rand(0,$this->codeImageHeight - 15);
			imagechar($this->codeImage, 5, $x, $y, $this->cecCode[$i], $bg_color);
		}
	}
	
	/**
	 * 把验证码的值写入session
	 *
	 * @param string $sname
	 */
	function writeSession($sname) {
		session_start();
		session_register($sname);
		$_SESSION[$sname] = md5($this->cecCode);
	}

	/**
	 * 输出验证码图片
	 *
	 * @param integer $width
	 * @param integer $height
	 * @param integer $num
	 * @param string $sname
	 */
	function outCodeImage($width = '', $height = '' ,$num = '', $sname = 'code') {
		if($width !== '' || $height !== '') {
			$this->setCodeImage($width, $height);
		}
		$this->outHeader();
		$this->createCode();
		$this->createImage();
		$this->setDisturbColor();
		$this->writeCodeToImage($num);
		$this->writeSession($sname);
		imagepng($this->codeImage);
		imagedestroy($this->codeImage);
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...