小小的php验证码程序(拿出来晒晒)
来源:互联网 发布:觉醒三少成长数据对比 编辑:程序博客网 时间:2024/04/27 15:42
1.
验证码文件类:CreateImg.class.php
<?php
/**
* @author HeHui
* @date 2010-7-30
* @email hehui0924@foxmail.com
* @验证码文件类
* int function imagecolorallocate(resource image, int red, int green, int blue) //为一幅图像分配颜色
* bool function imagefilledrectangle(resource image, int x1, int y1, int x2, int y2, int color) //画一矩形并填充
* bool function imagerectangle(resource image, int x1, int y1, int x2, int y2, int col) //画一个矩形
* bool function imagesetpixel(resource image, int x, int y, int color) //画一个单一像素
*/
class ValidationCode
{
private $width,$height,$codenum;
public $checkcode; //产生的验证码
private $checkimage; //验证码图片
private $disturbColor = ''; //干扰像素
/*
* 参数:(宽度,高度,字符个数)
*/
function __construct($width='80',$height='20',$codenum='4')
{
$this->width=$width;
$this->height=$height;
$this->codenum=$codenum;
}
function outImg()
{
//输出头
$this->outFileHeader();
//产生验证码
$this->createCode();
//产生图片
$this->createImage();
//设置干扰像素
$this->setDisturbColor();
//往图片上写验证码
$this->writeCheckCodeToImage();
imagepng($this->checkimage);
imagedestroy($this->checkimage);
}
/*
* @brief 输出头
*/
private function outFileHeader()
{
header ("Content-type: image/png");
}
/**
* 产生验证码
*/
private function createCode()
{
$this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum));
}
/**
* 产生验证码图片
*/
private function createImage()
{
$this->checkimage = @imagecreate($this->width,$this->height);
$back = imagecolorallocate($this->checkimage,255,255,255);
$border = imagecolorallocate($this->checkimage,0,0,0);
imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底
imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色边框
}
/**
* 设置图片的干扰像素
*/
private function setDisturbColor()
{
for ($i=0;$i<=200;$i++)
{
$this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor);
}
}
/**
*
* 在验证码图片上逐个画上验证码
*
*/
private function writeCheckCodeToImage()
{
for ($i=0;$i<=$this->codenum;$i++)
{
$bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255));
$x = floor($this->width/$this->codenum)*$i;
$y = rand(0,$this->height-15);
imagechar ($this->checkimage, rand(5,8), $x, $y, $this->checkcode[$i], $bg_color);
}
}
function __destruct()
{
unset($this->width,$this->height,$this->codenum);
}
}
?>
2.
文件名:imgcode.php
<?php
session_start();
require_once('CreateImg.class.php');
$image = new ValidationCode('80','20','4'); //图片长度、宽度、字符个数
$image->outImg();
$_SESSION['validationcode'] = $image->checkcode; //存贮验证码到 $_SESSION 中
?>
3.
文件名:index.php
<?php
session_start();
$test = $_POST['test'];
$test = strtoupper(trim($test));
$submit = $_POST['submit'];
if(isset($submit)){
if($test==$_SESSION['validationcode']){
echo 'true';
} else {
echo 'false';
}
}
?>
<html>
<head>
<title>Image</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script language="javascript">
function newgdcode(obj,url) {
obj.src = url+ '?nowtime=' + new Date().getTime();
//后面传递一个随机参数,否则在IE7和火狐下,不刷新图片
}
</script>
<body>
<img src="imgcode.php" alt="看不清楚,换一张" align="absmiddle" style="cursor: pointer;" onClick="javascript:newgdcode(this,this.src);" />
<form method="POST" name="form1" action="index.php">
<input type="text" name="test">
<br />
<input type="submit" name="submit" value="提交">
</form>
</body>
</head>
</html>
- 小小的php验证码程序(拿出来晒晒)
- 把word文档中的图片拿出来的小小经历
- N年前的文章了,拿出来晒晒吧...... 测试跟踪工具Bugzilla介绍
- 有朋友说liigo的名片不错,拿出来晒晒,看图
- 在公司工作做第一个项目的收获,拿出来晒晒。
- H3CNE、H3CSE认证考试亲身奋斗过程的心得(拿出来晒晒)
- 东西放久了会发霉的,源代码在线注释网站也一样,拿出来晒晒~~~
- 虽然没人回答 还是想拿出来晒晒!!
- 一个验证码图片生成(刚写的,拿出来分享)
- 一天天过得很快啊,今天大家都干了啥?我们拿出来晒晒
- 一天天过得很快啊,今天大家都干了啥?我们拿出来晒晒
- 晒晒我的程序人生
- 最近打算把我以前写的程序,拿出来免费发放出去
- 也来晒晒的我的学习成绩
- 拿出证据来,缴枪的不杀
- 漂亮的代码,忍不住拿出来
- 小小的表单验证
- PHP验证码程序
- 时间计划7步法
- 为什么要使用spring事务管理
- android开发过程中遇到的一些问题汇总
- jms 消息的确认模式
- 总结一下这一年来的心得体会
- 小小的php验证码程序(拿出来晒晒)
- JNI Java class与C struct对应
- Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务
- 企业级管理软件快速开发平台-极致业务基础平台
- 制作可以直接更新已经安装程序的升级包
- 最全的Js复制链接代码
- Linux Kernel 技术手册笔记
- Map List排序
- 企业级管理软件快速开发平台-极致业务基础开发平台各模块介绍