验证码类validateCode
PHP验证码类,代码如下:
<?php
//验证码类
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
private $code;//验证码
private $codelen = 4;//验证码长度
private $width = 52;//宽度
private $height = 28;//高度
private $img;//图形资源句柄
private $font;//指定的字体
private $fontsize = 11;//指定字体大小
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);
}
}
使用方法:
1、先把验证码类保存为一个名为 validateCode.php 的文件;
2、新建一个名为 captcha.php 的文件进行调用该类;
captcha.php中的代码如下:
<?php
session_start();
require 'validateCode.php'; //先把类包含进来,实际路径根据实际情况进行修改。
$validate = new ValidateCode(); //实例化一个对象
$validate->doImg();
$_SESSION['validate_code'] = $validate->getCode();//验证码保存到SESSION中
在页面中调用验证码的代码如下:
<input type="text" name="validate" size="10"/>
<img title="点击刷新" src="./validateCode/captcha.php" onclick="this.src='./validateCode/captcha.php?'+Math.random();"></img> //src是验证码图片的调用路径,onclick是点击后重新生成一个验证码,如果不加上Math.random() 验证码就只能刷新一次,
验证码通过表单提交到后台后,代码如下:
if(isset($_REQUEST["validate"])){
$validate=$_REQUEST["validate"];
if($validate!=$_SESSION["validate_code"]){
exit('验证码错误');
}else{
echo '验证码正确';
}
}
附验证码图片:
验证码类validateCode的更多相关文章
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- 一个漂亮的php验证码类(分享)
直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...
- ThinkPHP - 扩展个人类库 - 以验证码类为例子
首先,在项目目录下创建Class文件夹,用于存储个人类文件. 之后建立Data目录存放所需字体文件,其他的数据也可以放在这个文件夹下. 然后再Conf文件夹下创建verify.php配置文件. 在co ...
- 建立一个漂亮的PHP验证码类文件及调用方式
//验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/ ...
- 一个漂亮的php验证码类
一个漂亮的php验证码类(分享) 作者: 字体:[增加 减小] 类型:转载 下面小编就为大家分享一个漂亮的php验证码类.需要的朋友可以过来参考下 直接上代码: 复制代码 代码如下: //验证 ...
- THINKPHP源码学习--------验证码类
TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...
- ThinkPHP 3.2.3 加减乘法验证码类
ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...
- 【个人使用.Net类库】(4)验证码类
验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...
- PHP编写的图片验证码类文件分享方法
适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...
随机推荐
- LOCK TABLES 和 UNLOCK TABLES
MySQLdump的时LOCK TABLES 和 UNLOCK TABLES 在mysqldump后的数据中会发现有 LOCK TABLES tables_name WRITE;和结尾处有 UNLOC ...
- IIS异常
http 错误 500.19 - internal server error 今天发布wcf到本地的IIS上,访问时出现了500.19错误.有效解决办法:是因为IIS没有安装完全,把能勾选的全部勾选上 ...
- mysql中cast() 和convert()的用法讲解
一.在mysql操作中我们经常需要对数据进行类型转换.此时我们应该使用的是cast()或convert(). 二.两者的对比 相同点:都是进行数据类型转换,实现的功能基本等同 不同点:两者的语法不同, ...
- java抽象类详解
前言 在没讲抽象类之前 我们先来看看 final关键字 final 修饰符 可以修饰 类.属性.方法 修饰类时 表示该类不能被继承 其他特征 跟普通的类一样 修饰 属性时 表示 改属性不能改变 ...
- Introduction to boundary integral equations in BEM
Boundary element method (BEM) is an effective tool compared to finite element method (FEM) for resol ...
- 如何访问https的网站?-【httpclient】
备注:本处代码使用groovy和httpclient4.3作为例子进行讲述 在普通方式下,当使用httpclient进行访问某个网站时,大致使用如下的代码进行访问: CloseableHttpClie ...
- Python_copy_深浅拷贝
对于数字和字符串来说,无论是‘’赋值‘’还是‘’深拷贝‘’还是‘’浅拷贝‘’都是指向的同一个地址 深浅拷贝是copy类下的方法,创建方式为 import copy copy.copy() #浅拷贝 c ...
- postgresql 查询某一个表中的所有字段
select * from information_schema.columns where table_schema='public' and table_name='表名称 ';
- python--json、jsonpath
1.遇到一个问题:android返回的基本都是标准的json格式,当我们想要对层层嵌套的json中找到自己想要的字段并进行校验时 难道需要一层一层的解析?? 2.使用jsonpath list_3={ ...
- Codeforces 264C Choosing Balls 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF264C.html 题目传送门 - CF264C 题意 给定一个有 $n$ 个元素的序列,序列的每一个元素是个 ...