php实现点击可刷新验证码


Posted in PHP onNovember 07, 2015

本文实例为大家分享了php点击可刷新验证码的具体代码,供大家参考,具体内容如下

验证码类文件 CreateImg.class.php

<?php 
 
class ValidationCode 
{ 
private $width,$height,$codenum; 
public $checkcode;  //产生的验证码 
private $checkimage; //验证码图片 
private $disturbColor = ''; //干扰像素 
 
function __construct($width='80',$height='20',$codenum='4') 
{ 
 $this->width=$width; 
 $this->height=$height; 
 $this->codenum=$codenum; 
} 
function outImg() 
{ 
 //输出头 
 $this->outFileHeader(); 
 //产生验证码 
 $this->createCode(); 
 
 //产生图片 
 $this->createImage(); 
 //设置干扰像素 
 $this->setDisturbColor(); 
 //往图片上写验证码 
 $this->writeCheckCodeToImage(); 
 imagepng($this->checkimage); 
 imagedestroy($this->checkimage); 
} 
 
private function outFileHeader() 
{ 
 header ("Content-type: image/png"); 
} 
 
private function createCode() 
{ 
 $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum)); 
} 
 
private function createImage() 
{ 
 $this->checkimage = @imagecreate($this->width,$this->height); 
 $back = imagecolorallocate($this->checkimage,255,255,255); 
 $border = imagecolorallocate($this->checkimage,0,0,0); 
 imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底 
 imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色边框 
} 
 
private function setDisturbColor() 
{ 
 for ($i=0;$i<=200;$i++) 
 { 
 $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255)); 
 imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor); 
 } 
} 
 
private function writeCheckCodeToImage() 
{ 
 for ($i=0;$i<=$this->codenum;$i++) 
 { 
 $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255)); 
 $x = floor($this->width/$this->codenum)*$i; 
 $y = rand(0,$this->height-15); 
 imagechar ($this->checkimage, rand(5,8), $x, $y, $this->checkcode[$i], $bg_color); 
 } 
} 
function __destruct() 
{ 
 unset($this->width,$this->height,$this->codenum); 
} 
} 
?>

包含文件imgcode.php

<?php 
session_start(); 
require_once('CreateImg.class.php'); 
$image = new ValidationCode('80','20','4'); //图片长度、宽度、字符个数 
$image->outImg(); 
$_SESSION['validationcode'] = $image->checkcode; //存贮验证码到 $_SESSION 中 
?>

前台文件 demo.php

?php 
session_start(); 
$test = $_POST['test']; 
$test = strtoupper(trim($test)); 
$submit = $_POST['submit']; 
if(isset($submit)){ 
if($test==$_SESSION['validationcode']){ 
 echo 'true'; 
} else { 
 echo 'false'; 
} 
} 
?> 
 
<html> 
<head> 
<title>Image</title> 
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<script language="javascript"> 
function newgdcode(obj,url) { 
obj.src = url+ '?nowtime=' + new Date().getTime(); 
//后面传递一个随机参数,否则在IE7和火狐下,不刷新图片 
} 
</script> 
<body> 
<img src="imgcode.php" alt="看不清楚,换一张" align="absmiddle" style="cursor: pointer;" onclick="javascript:newgdcode(this,this.src);" /> 
<form method="POST" name="form1" action="image.php"> 
<input type="text" name="test"> 
<br /> 
<input type="submit" name="submit" value="提交"> 
</form> 
</body> 
</head> 
</html>

以上就是为大家介绍的php点击验证码即可刷新的全部代码,希望对大家的学习有所帮助。

PHP 相关文章推荐
一个用于mysql的数据库抽象层函数库
Oct 09 PHP
PHP4与PHP3中一个不兼容问题的解决方法
Oct 09 PHP
用php实现批量查询清除一句话后门的代码
Jan 20 PHP
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
May 15 PHP
php无限分类且支持输出树状图的详细介绍
Jun 19 PHP
PHP swfupload图片上传的实例代码
Sep 30 PHP
PHP中的多行字符串传递给JavaScript的两种方法
Jun 19 PHP
ThinkPHP模版引擎之变量输出详解
Dec 05 PHP
PHP开发中常用的十个代码样例
Feb 02 PHP
深入研究PHP中的preg_replace和代码执行
Aug 15 PHP
PHP单例模式数据库连接类与页面静态化实现方法
Mar 20 PHP
Laravel项目中timeAgo字段语言转换的改善方法示例
Sep 16 PHP
PHP中SSO Cookie登录分析和实现
Nov 06 #PHP
使用PHP similar text计算两个字符串相似度
Nov 06 #PHP
PHP正则表达式之捕获组与非捕获组
Nov 06 #PHP
php创建无限级树型菜单
Nov 05 #PHP
详解PHP中instanceof关键字及instanceof关键字有什么作用
Nov 05 #PHP
PHP递归创建多级目录
Nov 05 #PHP
PHP中的静态变量及static静态变量使用详解
Nov 05 #PHP
You might like
基于PHP异步执行的常用方式详解
2013/06/03 PHP
windows的文件系统机制引发的PHP路径爆破问题分析
2014/07/28 PHP
PHP中is_dir()函数使用指南
2015/05/08 PHP
JavaScript 学习笔记(四)
2009/12/31 Javascript
基于jquery的$.ajax async使用
2011/10/19 Javascript
JQuery调用绑定click事件的3种写法
2015/03/28 Javascript
Angularjs 自定义服务的三种方式(推荐)
2016/08/02 Javascript
AngularJS+Bootstrap实现多文件上传与管理
2016/11/08 Javascript
AngularJS使用angular.bootstrap完成模块手动加载的方法分析
2017/01/19 Javascript
JavaScript获取select中text值的方法
2017/02/13 Javascript
seajs下require书写约定实例分析
2018/05/16 Javascript
js实现动态增加文件域表单功能
2018/10/22 Javascript
浅谈Vue的响应式原理
2019/05/30 Javascript
vue登录页面cookie的使用及页面跳转代码
2019/07/10 Javascript
vue-model实现简易计算器
2020/08/17 Javascript
vue自定义组件实现双向绑定
2021/01/13 Vue.js
Python3指定路径寻找符合匹配模式文件
2015/05/22 Python
django rest framework之请求与响应(详解)
2017/11/06 Python
Python中join函数简单代码示例
2018/01/09 Python
Python闭包之返回函数的函数用法示例
2018/01/27 Python
python中实现数组和列表读取一列的方法
2018/04/03 Python
python算法与数据结构之单链表的实现代码
2019/06/27 Python
Python读写操作csv和excle文件代码实例
2020/03/16 Python
python实现横向拼接图片
2020/03/23 Python
Python如何实现大型数组运算(使用NumPy)
2020/07/24 Python
python基于opencv 实现图像时钟
2021/01/04 Python
ALDO加拿大官网:加拿大女鞋品牌
2018/12/22 全球购物
中学校庆方案
2014/03/17 职场文书
女生节标语
2014/06/26 职场文书
人大调研汇报材料
2014/08/14 职场文书
户外亲子活动总结
2015/05/08 职场文书
2016年元旦主持词
2015/07/06 职场文书
小学班级口号大全
2015/12/25 职场文书
2016年全国助残日活动总结
2016/04/01 职场文书
中国古风插画师排行榜:夏达第一,第三是阴阳师姑获鸟皮肤创作者
2022/03/18 国漫
Android开发手册TextInputLayout样式使用示例
2022/06/10 Java/Android