PHP中的验证码类(完善验证码)
运行结果:

<!--vcode.class.php-->
<?php
class Vcode {
private $width; //宽
private $height; //高
private $num; //数量
private $code; //验证码
private $img; //图像的资源
//构造方法, 三个参数
function __construct($width=80, $height=20, $num=4) {
$this->width = $width;
$this->height = $height;
$this->num = $num;
$this->code = $this->createcode(); //调用自己的方法
}
//获取字符的验证码, 用于保存在服务器中
function getcode() {
return $this->code;
}
//输出图像
function outimg() {
//创建背景 (颜色, 大小, 边框)
$this->createback();
//画字 (大小, 字体颜色)
$this->outstring();
//干扰元素(点, 线条)
$this->setdisturbcolor();
//输出图像
$this->printimg();
}
//创建背景
private function createback() {
//创建资源
$this->img = imagecreatetruecolor($this->width, $this->height);
//设置随机的背景颜色
$bgcolor = imagecolorallocate($this->img, rand(225, 255), rand(225, 255), rand(225, 255));
//设置背景填充
imagefill($this->img, 0, 0, $bgcolor);
//画边框
$bordercolor = imagecolorallocate($this->img, 0, 0, 0);
imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, $bordercolor);
}
//画字
private function outstring() {
for($i=0; $i<$this->num; $i++) {
$color= imagecolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128));
$fontsize=rand(3,5); //字体大小
$x = 3+($this->width/$this->num)*$i; //水平位置
$y = rand(0, imagefontheight($fontsize)-3);
//画出每个字符
imagechar($this->img, $fontsize, $x, $y, $this->code{$i}, $color);
}
}
//设置干扰元素
private function setdisturbcolor() {
//加上点数,100个点
for($i=0; $i<100; $i++) {
$color= imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->img, rand(1, $this->width-2), rand(1, $this->height-2), $color);
}
//加线条,10条线
for($i=0; $i<10; $i++) {
$color= imagecolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));
imagearc($this->img,rand(-10, $this->width+10), rand(-10, $this->height+10), rand(30, 300), rand(30, 300), 55,44, $color);
}
}
//输出图像
private function printimg() {
if (imagetypes() & IMG_GIF) {
header("Content-type: image/gif");
imagegif($this->img);
} elseif (function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagegif($this->img);
} elseif (imagetypes() & IMG_PNG) {
header("Content-type: image/png");
imagegif($this->img);
} else {
die("No image support in this PHP server");
}
}
//生成验证码字符串
private function createcode() {
$codes = "3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";
$code = "";
for($i=0; $i < $this->num; $i++) {
$code .=$codes{rand(0, strlen($codes)-1)};
}
return $code;
}
//用于自动销毁图像资源
function __destruct() {
imagedestroy($this->img);
}
}
<!--reg.php-->
<?php
session_start();
if(isset($_POST['dosubmit'])) {
if(strtoupper($_SESSION['code']) == strtoupper($_POST['code']) ) {
echo "输入成功!<br>";
}else{
echo "输入不对!<br>";
}
}
?>
<body>
<form action="reg.php" method="post">
username: <input type="text" name="username"> <br>
password: <input type="password" name="password"> <br>
code: <input type="text" onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()" size="6" name="code">
<img src="code.php" onclick="this.src='code.php?'+Math.random()" /> <br>
<input type="submit" name="dosubmit" value="登 录"> <br>
</form>
</body>
<!--code.php内容-->
<?php
//开启session
session_start();
include "vcode.class.php";
//构造方法
$vcode = new Vcode(80, 30, 4);
//将验证码放到服务器自己的空间保存一份
$_SESSION['code'] = $vcode->getcode();
//将验证码图片输出
$vcode->outimg();
PHP中的验证码类(完善验证码)的更多相关文章
- ThinkPHP中使用Verify类生产验证码不显示的原因
今天在做网站部署的时候,发现登录页面的验证码显示不出来了,而且不报任何错误. 直接通过url访问该操作也不能显示. 后来在网上查找了一些解决方法. 在调用$verify = new \Think\Ve ...
- PHP中的验证码类(验证码功能设计之二)
运行结果: <!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 ...
- PHP中的验证码类(验证码功能设计之一)
<!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 priva ...
- CI框架中,扩展验证码类。
使用CI框架的朋友,应该都知道CI框架的的验证码辅助函数,不太好用.它需要写入到数据库中,然后再进行比对. 大家在实际项目中,好像不会这样去使用,因为会对数据库造成一定的压力. 所以,我们还是利用se ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- 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 ...
- 一个漂亮的php验证码类(分享)
直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...
- PHP编写的图片验证码类文件分享方法
适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...
- laravel加入验证码类几种方法 && Laravel引入第三方库的方法
1,使用require , inlcude 的方法将验证码类文件包含进来,再进行new 2,将验证码类文件放于Http目录下面,也就是和控制器controller放在一个目录下面,在验证码类文件中加上 ...
随机推荐
- MySql面试题、知识汇总、牛客网SQL专题练习
点击名字直接跳转到链接: Linux运维必会的100道MySql面试题之(一) Linux运维必会的100道MySql面试题之(二) Linux运维必会的100道MySql面试题之(三) Linux运 ...
- C#算术运算符
一.C#算术运算符 C#语言的算术运算符主要用于数学计算中. 二.示例 using System;using System.Collections.Generic;using System.Linq; ...
- 2d游戏中求出一个向量的两个垂直向量
function cc.exports.VerticalVector(vec)--求出两个垂直向量 local result = {} result[1] = cc.p(vec.y/vec.x,-1) ...
- C# 获取Google Chrome的书签
其实这个很简单,就是读取一个在用户目录里面的一个Bookmarks文件就好了. 先建立几个实体类 public class GoogleChrome_bookMark_meta_info { publ ...
- MySQL - INSERT 集合
范例1: INSERT INTO t_table SELECT ot.* FROM t_other_table ot WHERE ot.is_sent = ? and ot.insert_time & ...
- Qt的由来和发展
一.Qt的由来 Haavard Nord 和Eirik Chambe-Eng于1991年开始开发"Qt",1994年3月4日创立公司,早名为Quasar Technologies, ...
- Python基础(三)—— print()格式化输出变量
先举一个简单的例子说明: name = 'Jack' answer = input('你好,%s '%(name) + '你认识 Sean 不, 输入 yes or no\n') print('Sea ...
- selenium2截图ScreenShot的使用
截图是做测试的基本技能,在有BUG的地方,截个图,保留失败的证据,也方便去重现BUG.所以,在自动化的过程中,也要能截图,也要能在我们想要截取的地方去截图,且能在错误产生时,自动的截图. 示例: 脚本 ...
- CDH4 journalnode方式手工安装手册之二
一. Hadoop配置修改 修改core-site.xml文件 <configuration> <property> ...
- Python中的魔术方法详解(双下方法)
介绍 在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”,中文称『魔术方法』,例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中 ...