一个漂亮的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更新mysql后获取影响的行数发生异常解决方法
Mar 28 PHP
PHP登陆后跳转到登陆前页面实现思路及代码
Jan 17 PHP
php实现aes加密类分享
Feb 16 PHP
PHP中多维数组的foreach遍历示例
Jun 13 PHP
PHP文件读取功能的应用实例
May 08 PHP
php商品对比功能代码分享
Sep 24 PHP
纯PHP代码实现支付宝批量付款
Dec 24 PHP
thinkPHP下的widget扩展用法实例分析
Dec 26 PHP
高质量PHP代码的50个实用技巧必备(下)
Jan 22 PHP
PHP 闭包详解及实例代码
Sep 28 PHP
Laravel中的Auth模块详解
Aug 17 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
利用discuz自带通行证整合dedecms的方法以及文件下载
2007/03/06 PHP
PHP利用REFERER根居访问来地址进行页面跳转
2013/09/28 PHP
javascript学习之闭包分析
2010/12/02 Javascript
Javascript 页面模板化很多人没有使用过的方法
2012/06/05 Javascript
解析ScrollPic在ie8下只滚动一遍,然后变为空白 ie6,ie7,chrome,firefox正常
2013/06/26 Javascript
javascript动态判断html元素并执行不同的操作
2014/06/16 Javascript
Node.js node-schedule定时任务隔多少分钟执行一次的方法
2015/02/10 Javascript
深入理解JavaScript编程中的同步与异步机制
2015/06/24 Javascript
简单谈谈javascript Date类型
2015/09/06 Javascript
Vue.directive()的用法和实例详解
2018/03/04 Javascript
React Native基础入门之初步使用Flexbox布局
2018/07/02 Javascript
react高阶组件添加和删除props
2019/04/26 Javascript
小程序实现密码输入框
2020/11/16 Javascript
[46:00]Ti4 冒泡赛第二轮LGD vs C9 2
2014/07/14 DOTA
[01:08]DOTA2次级职业联赛 - Shield战队宣传片
2014/12/01 DOTA
Python中shutil模块的常用文件操作函数用法示例
2016/07/05 Python
Python进行数据提取的方法总结
2016/08/22 Python
Python编程生成随机用户名及密码的方法示例
2017/05/05 Python
python 处理dataframe中的时间字段方法
2018/04/10 Python
解决Pycharm界面的子窗口不见了的问题
2019/01/17 Python
django 通过url实现简单的权限控制的例子
2019/08/16 Python
Python PIL图片添加字体的例子
2019/08/22 Python
python实现3D地图可视化
2020/03/25 Python
地球上最先进的胡子和头发修剪器:Bevel
2018/01/23 全球购物
Wedgwood英国官方网站:英式精致骨瓷餐具、礼品与生活精品,源于1759年
2019/09/02 全球购物
大学生自我鉴定范文模板
2014/01/21 职场文书
宿舍违规用电检讨书
2014/02/16 职场文书
王老吉广告词
2014/03/20 职场文书
2014幼儿园中班工作总结
2014/11/10 职场文书
2015医德医风个人工作总结
2015/04/02 职场文书
业务内勤岗位职责
2015/04/13 职场文书
围城读书笔记
2015/06/26 职场文书
学雷锋活动简报
2015/07/20 职场文书
MySQL配置主从服务器(一主多从)
2021/08/07 MySQL
简单聊聊Vue中的计算属性和属性侦听
2021/10/05 Vue.js
Python利用FlashText算法实现替换字符串
2022/03/31 Python