一个漂亮的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 相关文章推荐
PHP 中文处理技巧
Apr 25 PHP
PHP版国家代码、缩写查询函数代码
Aug 14 PHP
PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
Dec 16 PHP
php中利用str_pad函数生成数字递增形式的产品编号
Sep 30 PHP
PHP闭包实例解析
Sep 08 PHP
字符串长度函数strlen和mb_strlen的区别示例介绍
Sep 09 PHP
PHP从FLV文件获取视频预览图的方法
Mar 12 PHP
PHP人民币金额转大写实例代码
Oct 02 PHP
CI框架AR操作(数组形式)实现插入多条sql数据的方法
May 18 PHP
php中__toString()方法用法示例
Dec 07 PHP
PHP实现阿里大鱼短信验证的实例代码
Jul 10 PHP
PHP判断当前使用的是什么浏览器(推荐)
Oct 27 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
php 冒泡排序 交换排序法
2011/05/10 PHP
PHP判断手机是IOS还是Android
2015/12/09 PHP
CodeIgniter扩展核心类实例详解
2016/01/20 PHP
PHP的mysqli_sqlstate()函数讲解
2019/01/23 PHP
jQuery中对未来的元素绑定事件用bind、live or on
2014/04/17 Javascript
jQuery应用之jQuery链用法实例
2015/01/19 Javascript
js实现点击图片改变页面背景图的方法
2015/02/28 Javascript
JavaScript原型及原型链终极详解
2016/01/04 Javascript
JavaScript类型系统之正则表达式
2016/01/05 Javascript
AngularJS  $on、$emit和$broadcast的使用
2016/09/05 Javascript
JavaScript的六种继承方式(推荐)
2017/06/26 Javascript
Angular 如何使用第三方库的方法
2018/04/18 Javascript
vue左侧菜单,树形图递归实现代码
2018/08/24 Javascript
小程序图片长按识别功能的实现方法
2018/08/30 Javascript
this在vue和小程序中的使用详解
2019/01/28 Javascript
Python实现自动添加脚本头信息的示例代码
2016/09/02 Python
详谈pandas中agg函数和apply函数的区别
2018/04/20 Python
python的set处理二维数组转一维数组的方法示例
2019/05/31 Python
Python爬取智联招聘数据分析师岗位相关信息的方法
2019/08/13 Python
python 下 CMake 安装配置 OPENCV 4.1.1的方法
2019/09/30 Python
python语言线程标准库threading.local解读总结
2019/11/10 Python
Python 中判断列表是否为空的方法
2019/11/24 Python
css3.0 图形构成实例练习二
2013/03/19 HTML / CSS
澳大利亚旅游网站:Lastminute
2017/08/07 全球购物
办公室文秘自我评价
2013/09/21 职场文书
工作自我评价分享
2013/12/01 职场文书
十八大报告观后感
2014/01/28 职场文书
经理助理岗位职责
2014/03/05 职场文书
学术会议主持词
2014/03/17 职场文书
民主评议党员工作总结
2014/10/20 职场文书
思想作风建设心得体会
2014/10/22 职场文书
2014村党支部书记党建工作汇报材料
2014/11/02 职场文书
北京英文导游词
2015/02/12 职场文书
观后感格式
2015/06/19 职场文书
教师正风肃纪心得体会
2016/01/15 职场文书
css3实现的加载动画效果
2021/04/07 HTML / CSS