一个漂亮的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部分常见问题总结
Oct 09 PHP
PHP $_SERVER详解
Jan 16 PHP
深入extjs与php参数交互的详解
Jun 25 PHP
PHP生成迅雷、快车、旋风等软件的下载链接代码实例
May 12 PHP
php实现二进制和文本相互转换的方法
Apr 18 PHP
PHP多维数组遍历方法(2种实现方法)
Dec 10 PHP
php通过smtp邮件验证登陆的方法
May 11 PHP
thinkPHP5.0框架开发规范简介
Mar 25 PHP
Laravel如何使用数据库事务及捕获事务失败后的异常详解
Oct 23 PHP
PHP7新特性之抽象语法树(AST)带来的变化详解
Jul 17 PHP
PHP实现websocket通信的方法示例
Aug 28 PHP
PHP+Ajax简单get验证操作示例
Mar 02 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
NOT NULL 和NULL
2007/01/15 PHP
php上传文件中文文件名乱码的解决方法
2013/11/01 PHP
PHP弱类型的安全问题详细总结
2016/09/25 PHP
PHP PDOStatement::debugDumpParams讲解
2019/01/30 PHP
JavaScript Title、alt提示(Tips)实现源码解读
2010/12/12 Javascript
js显示时间 js显示最后修改时间
2013/01/02 Javascript
javascript解决innerText浏览器兼容问题思路代码
2013/05/17 Javascript
一款由jquery实现的整屏切换特效
2014/09/15 Javascript
jQuery中andSelf()方法用法实例
2015/01/08 Javascript
对JavaScript客户端应用编程的一些建议
2015/06/24 Javascript
详解WordPress开发中get_current_screen()函数的使用
2016/01/11 Javascript
理解js对象继承的N种模式
2016/01/25 Javascript
BootStrap 图标icon符号图标glyphicons不正常显示的快速解决办法
2016/12/08 Javascript
使用Bootstrap + Vue.js实现添加删除数据示例
2017/02/27 Javascript
详解vue2 $watch要注意的问题
2017/09/08 Javascript
使用JS中的Replace()方法遇到的问题小结
2017/10/20 Javascript
JS实现同一DOM元素上onClick事件与onDblClick事件并存的解决方法
2018/06/07 Javascript
在Vue项目中取消ESLint代码检测的步骤讲解
2019/01/27 Javascript
angular使用md5,CryptoJS des加密的方法
2019/06/03 Javascript
VUE 单页面使用 echart 窗口变化时的用法
2020/07/30 Javascript
nodeJs项目在阿里云的简单部署
2020/11/27 NodeJs
Python实现二分查找算法实例
2015/05/26 Python
详解Python各大聊天系统的屏蔽脏话功能原理
2016/12/01 Python
python_array[0][0]与array[0,0]的区别详解
2020/02/18 Python
如何用python处理excel表格
2020/06/09 Python
YOOX美国官方网站:全球著名的多品牌时尚网络概念店
2016/09/11 全球购物
技术总监岗位职责
2013/12/05 职场文书
时尚休闲吧创业计划书
2014/01/25 职场文书
公立医院改革实施方案
2014/03/14 职场文书
殡葬服务心得体会
2014/09/11 职场文书
落实八项规定专题民主生活会对照检查材料
2014/09/15 职场文书
2014最新预备党员思想汇报范文:中国梦,我的梦
2014/10/25 职场文书
2014年乡镇妇联工作总结
2014/12/02 职场文书
司考复习计划
2015/01/19 职场文书
质量承诺书格式范文
2015/04/28 职场文书
Python数据可视化之用Matplotlib绘制常用图形
2021/06/03 Python