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


Posted in PHP onAugust 06, 2013

直接上代码:

//验证码类
class ValidateCode {
 private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
 private $code;//验证码
 private $codelen = 4;//验证码长度
 private $width = 130;//宽度
 private $height = 50;//高度
 private $img;//图形资源句柄
 private $font;//指定的字体
 private $fontsize = 20;//指定字体大小
 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);
 }
}

输出实例:一个漂亮的php验证码类(分享)

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

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

3、引用到页面中,代码如下:
<img  title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>

4、一个完整的验证页面,代码如下:
<?php
session_start();
//在页首先要开启session,
//error_reporting(2047);
session_destroy();
//将session去掉,以每次都能取新的session值;
//用seesion 效果不错,也很方便
?>
<html>
<head>
<title>session 图片验证实例</title>
<style type="text/css">
#login p{
margin-top: 15px;
line-height: 20px;
font-size: 14px;
font-weight: bold;
}
#login img{
cursor:pointer;
}
form{
margin-left:20px;
}
</style>
</head> 
<body> 
<form id="login" action="" method="post">
<p>此例为session验证实例</p>
<p>
<span>验证码:</span>
<input type="text" name="validate" value="" size=10> 
<img  title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
</p>
<p>
<input type="submit">
</p>
</form>
<?php
//打印上一个session;
//echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";
$validate="";
if(isset($_POST["validate"])){
$validate=$_POST["validate"];
echo "您刚才输入的是:".$_POST["validate"]."<br>状态:";
if($validate!=$_SESSION["authnum_session"]){
//判断session值与用户输入的验证码是否一致;
echo "<font color=red>输入有误</font>"; 
}else{
echo "<font color=green>通过验证</font>"; 
}
} 
?>

完整demo下载:demo
PHP 相关文章推荐
分页显示Oracle数据库记录的类之一
Oct 09 PHP
PHP伪造referer实例代码
Sep 20 PHP
php 文件上传实例代码
Apr 19 PHP
用来解析.htgroup文件的PHP类
Sep 05 PHP
PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
Oct 05 PHP
php生成EAN_13标准条形码实例
Nov 13 PHP
php中fgetcsv()函数用法实例
Nov 28 PHP
浅谈json_encode用法
Mar 05 PHP
PHP基于单例模式实现的mysql类
Jan 09 PHP
php微信公众账号开发之前五个坑(一)
Sep 18 PHP
php插入mysql数据返回id的方法
May 31 PHP
php日志函数error_log用法实例分析
Sep 23 PHP
如何在php中正确的使用json
Aug 06 #PHP
PHP 线程安全与非线程安全版本的区别深入解析
Aug 06 #PHP
浅析php中三个等号(===)和两个等号(==)的区别
Aug 06 #PHP
解析php中如何调用用户自定义函数
Aug 06 #PHP
使用php实现截取指定长度
Aug 06 #PHP
php 如何获取数组第一个值
Aug 06 #PHP
php number_format() 函数通过千位分组来格式化数字的实现代码
Aug 06 #PHP
You might like
初识Laravel
2014/10/30 PHP
PHP设计模式之建造者模式定义与用法简单示例
2018/08/13 PHP
Laravel 实现在Blade模版中使用全局变量代替路径的例子
2019/10/22 PHP
动态创建script在IE中缓存js文件时导致编码的解决方法
2014/05/04 Javascript
jQuery.holdReady()方法用法实例
2014/12/27 Javascript
jQuery中unwrap()方法用法实例
2015/01/16 Javascript
js实现内容显示并使用json传输数据
2016/03/16 Javascript
js自定义select下拉框美化特效
2016/05/12 Javascript
详谈JS中实现种子随机数及作用
2016/07/19 Javascript
AngularJS实现一次监听多个值发生的变化
2016/08/31 Javascript
Seajs是什么及sea.js 由来,特点以及优势
2016/10/13 Javascript
Bootstrap Modal遮罩弹出层(完整版)
2016/11/21 Javascript
AngularJS select加载数据选中默认值的方法
2018/02/28 Javascript
使用flow来规范javascript的变量类型
2019/09/12 Javascript
layui lay-verify form表单自定义验证规则详解
2019/09/18 Javascript
JavaScript Dom 绑定事件操作实例详解
2019/10/02 Javascript
详解JSON.stringify()的5个秘密特性
2020/05/26 Javascript
Vue如何实现验证码输入交互
2020/12/07 Vue.js
[34:39]Secret vs VG 2018国际邀请赛淘汰赛BO3 第二场 8.23
2018/08/24 DOTA
以Flask为例讲解Python的框架的使用方法
2015/04/29 Python
python写日志封装类实例
2015/06/28 Python
python 判断参数为Nonetype类型或空的实例
2018/10/30 Python
Python使用统计函数绘制简单图形实例代码
2019/05/15 Python
PyCharm2019安装教程及其使用(图文教程)
2019/09/29 Python
TensorFlow tf.nn.conv2d实现卷积的方式
2020/01/03 Python
python opencv 图像边框(填充)添加及图像混合的实现方法(末尾实现类似幻灯片渐变的效果)
2020/03/09 Python
python安装dlib库报错问题及解决方法
2020/03/16 Python
详解scrapy内置中间件的顺序
2020/09/28 Python
python pyg2plot的原理知识点总结
2021/02/28 Python
CSS3 简写animation
2012/05/10 HTML / CSS
使用HTML5的链接预取功能(link prefetching)给网站提速
2012/12/13 HTML / CSS
美国网上眼镜商城:Zenni Optical
2016/11/20 全球购物
七年级上册语文教学计划
2015/01/22 职场文书
php中pcntl_fork详解
2021/04/01 PHP
Go语言并发编程 sync.Once
2021/10/16 Golang
《总之就是很可爱》新作短篇动画《总之就是很可爱~制服~》将于2022年夏天播出
2022/04/07 日漫