一个漂亮的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 相关文章推荐
我的论坛源代码(八)
Oct 09 PHP
PHP中的integer类型使用分析
Jul 27 PHP
PHP常用的文件操作函数经典收藏
Apr 02 PHP
php将gd生成的图片缓存到memcache的小例子
Jun 05 PHP
PHP_Cooikes不同页面无法传递的解决方法
Mar 07 PHP
ThinkPHP连接数据库及主从数据库的设置教程
Aug 22 PHP
一个非常完美的读写ini格式的PHP配置类分享
Feb 12 PHP
PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function
Feb 05 PHP
PHP中检索字符串的方法分析【strstr与substr_count方法】
Feb 17 PHP
PHP基于新浪IP库获取IP详细地址的方法
May 04 PHP
PHP检查网站是否宕机的方法示例
Jul 24 PHP
laravel框架邮箱认证实现方法详解
Nov 22 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数组函数序列之sort() 对数组的元素值进行升序排序
2011/11/02 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
2013/06/20 PHP
Yii Framework框架获取分类下面的所有子类方法
2014/06/20 PHP
UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版
2015/12/08 PHP
jquery json 实例代码
2010/12/02 Javascript
js实现动态添加、删除行、onkeyup表格求和示例
2013/08/18 Javascript
用js通过url传参把数据从一个页面传到另一个页面
2014/09/01 Javascript
JavaScript字符串对象charAt方法入门实例(用于取得指定位置的字符)
2014/10/17 Javascript
jQuery替换textarea中换行的方法
2015/06/10 Javascript
JavaScript中Date.toSource()方法的使用教程
2015/06/12 Javascript
利用JavaScript脚本实现滚屏效果的方法
2015/07/07 Javascript
javascript实现省市区三级联动下拉框菜单
2015/11/17 Javascript
详解获取jq ul第一个li定位的四种解决方案
2016/11/23 Javascript
JS使用cookie实现只出现一次的广告代码效果
2017/04/22 Javascript
详解Angular2 之 结构型指令
2017/06/21 Javascript
详解webpack进阶之插件篇
2017/07/06 Javascript
使用bootstraptable插件实现表格记录的查询、分页、排序操作
2017/08/06 Javascript
详解解决使用axios发送json后台接收不到的问题
2018/06/27 Javascript
如何使用Javascript中的this关键字
2020/05/28 Javascript
python使用7z解压软件备份文件脚本分享
2014/02/21 Python
Python中使用dom模块生成XML文件示例
2015/04/05 Python
Python切片操作实例分析
2018/03/16 Python
Python开发的十个小贴士和技巧及长常犯错误
2018/09/27 Python
python实现简单多人聊天室
2018/12/11 Python
TensorFlow内存管理bfc算法实例
2020/02/03 Python
Python数据可视化处理库PyEcharts柱状图,饼图,线性图,词云图常用实例详解
2020/02/10 Python
python实现引用其他路径包里面的模块
2020/03/09 Python
3分钟看懂Python后端必须知道的Django的信号机制
2020/07/26 Python
中国领先的专业家电网购平台:国美在线
2016/12/25 全球购物
Bugatchi官方网站:男士服装在线
2019/04/10 全球购物
大学宣传委员竞选稿
2015/11/19 职场文书
导游词之四川熊猫基地
2020/01/13 职场文书
如何使用php生成zip压缩包
2021/04/21 PHP
SQL基础查询和LINQ集成化查询
2022/01/18 MySQL
Pillow图像处理库安装及使用
2022/04/12 Python
vue里使用create, mounted调用方法
2022/04/26 Vue.js