PHP验证码类,代码如下:

<?php
//验证码类
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
private $code;//验证码
private $codelen = 4;//验证码长度
private $width = 52;//宽度
private $height = 28;//高度
private $img;//图形资源句柄
private $font;//指定的字体
private $fontsize = 11;//指定字体大小
private $fontcolor;//指定字体颜色 //构造方法初始化
public function __construct() {
$this->font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片
} //生成随机码
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];
}
} //生成背景
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
} //生成文字
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
} //生成线条、雪花
private function createLine() {
//线条
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
} //雪花
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
} //输出
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
} //对外生成
public function doImg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
} //获取验证码
public function getCode() {
return strtolower($this->code);
}
}

使用方法:
1、先把验证码类保存为一个名为 validateCode.php 的文件;
2、新建一个名为 captcha.php 的文件进行调用该类;
captcha.php中的代码如下:

<?php
session_start();
require 'validateCode.php'; //先把类包含进来,实际路径根据实际情况进行修改。
$validate = new ValidateCode(); //实例化一个对象
$validate->doImg();
$_SESSION['validate_code'] = $validate->getCode();//验证码保存到SESSION中

在页面中调用验证码的代码如下:

<input type="text" name="validate" size="10"/>
<img title="点击刷新" src="./validateCode/captcha.php" onclick="this.src='./validateCode/captcha.php?'+Math.random();"></img> //src是验证码图片的调用路径,onclick是点击后重新生成一个验证码,如果不加上Math.random() 验证码就只能刷新一次,

验证码通过表单提交到后台后,代码如下:

if(isset($_REQUEST["validate"])){
$validate=$_REQUEST["validate"];
if($validate!=$_SESSION["validate_code"]){
exit('验证码错误');
}else{
echo '验证码正确';
}
}

附验证码图片:

验证码类validateCode的更多相关文章

  1. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  2. 一个漂亮的php验证码类(分享)

    直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...

  3. ThinkPHP - 扩展个人类库 - 以验证码类为例子

    首先,在项目目录下创建Class文件夹,用于存储个人类文件. 之后建立Data目录存放所需字体文件,其他的数据也可以放在这个文件夹下. 然后再Conf文件夹下创建verify.php配置文件. 在co ...

  4. 建立一个漂亮的PHP验证码类文件及调用方式

    //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/ ...

  5. 一个漂亮的php验证码类

    一个漂亮的php验证码类(分享)   作者: 字体:[增加 减小] 类型:转载 下面小编就为大家分享一个漂亮的php验证码类.需要的朋友可以过来参考下   直接上代码: 复制代码 代码如下: //验证 ...

  6. THINKPHP源码学习--------验证码类

    TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...

  7. ThinkPHP 3.2.3 加减乘法验证码类

    ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...

  8. 【个人使用.Net类库】(4)验证码类

    验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...

  9. PHP编写的图片验证码类文件分享方法

    适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...

随机推荐

  1. MyBatis - 10.MyBatis扩展

    1.PageHelpler分页插件使用 官方文档:中文 1.1 引入插件 1.1.1 引入的jar pagehelper-5.1.6.jar jsqlparser-1.2.jar 1.1.2 mave ...

  2. 前后台交互经常使用的技术汇总(后台:Java技术,前台:Js或者Jquery)

    1:由于针对特定的前后台交互用到的知识总结,所以不大量贴代码,主要给出思路,方便自己以后脑补和技术总结,当然也希望可以帮助到别人. 后台Json和其他格式转化,之前总结过Json和对象,集合,字符串的 ...

  3. springboot1.5.4 集成cxf完整实例

    WebService 服务端 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  4. [arc066f]Contest with Drinks Hard

    题目大意: 有一些物品,每个买了有代价. 如果存在一个极大区间[l,r]内的物品都被买了,这个区间长度为k,可以获得的收益是k*(k+1)/2. 现在若干次询问,每次问假如修改了某个物品的价格,最大收 ...

  5. bzoj4520【CQOI2016】K远点对

    题解: kd-tree裸题 对每个点维护最近的k个开个堆维护一下

  6. alpha冲刺6/10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:感恩节~ 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 设计了拍照界面和图片上传界面 沟通了前端进度 接下 ...

  7. python之string模块常量:数字,26个字母,标点符号,空白

    In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...

  8. Theorems for existence and uniqueness of variational problem

    Introduction Among simulation engineers, it is well accepted that the solution of a PDE can be envis ...

  9. C# 之 比较两个word文档的内容

    利用 Microsoft.Office.Interop.Word 组件进行比较.引入命名空间:using Word2013 = Microsoft.Office.Interop.Word; 代码如下: ...

  10. Ubuntu16.04下的modules模块编译加载

    一.首先编写对应的驱动程序的相关内容:(最简单的hello.c程序) #include<linux/init.h> #include<linux/module.h> MODUL ...