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放在一个目录下面,在验证码类文件中加上 ...
随机推荐
- Hibernate中session的save方法问题
今天在使用session.save(),进行插入数据操作时,一直没有成功,也没有报错.后来发现是因为没有创建事务,提交事务的原因 你对flush和commit的意思没有理解到:1,flush代表刷新, ...
- HTML5语义
语义通俗化为意义,也就是语义化的元素等于意义化的元素,看到这个元素的名称,就知道这个元素的意义,是拿来做什么用的,这就是HTML5的一个新特性,一个具有语义化的元素能够清楚的把元素的意义告诉浏览器和开 ...
- UpdatePanel中点击按钮Session过期跳转页面相关问题:Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息
使用 Response.Write("<script language=javascript>window.location.href='Login.aspx';</scr ...
- webgis技术在智慧城市综合治理网格化社会管理平台(综治平台)的应用
网格化社会管理平台功能:1 实有人口管理人口数据管理按照人口分类进行管理,分为常住人口.流动人口.特殊人群.弱势群体,功能包括人口信息管理.归口负责.人房关联.统计汇总.地图监管服务等功能.人口信 ...
- Some tricks
一 . \(2^i >\sum_{0}^{i - 1}2^i\) 二. 当概率非常小时,且答案允许范围内的误差.如与正确答案不超过\(2^{-6}\)即可. 选取一个较小的值,然后取min即可. ...
- numpy中常用的函数
1. power(x1, x2) 对x1中的每个元素求n次方.不会改变x1上午shape. 2. sum(a, axis=None, dtype=None, out=None, keepdims=Fa ...
- Ansible的使用和模块化深入
Ansible配置 配置文件:/etc/ansible/ansible.cfg [default] 默认配置 inventory = /etc/ansible/hosts主机清单 library = ...
- Unity基础-发布
PC BuildSetting File->BuildSetting Switch Platform Development Build是在调试模式下使用版本 Script Debugging调 ...
- PHP四种序列化方案
原文地址:https://t.ti-node.com/thread/... 数据的序列化是一个非常有用的功能,然而目测很多人跟我一样,在刚接触这玩意的时候压根就不理解这货色到底是干啥用的,反正老师说了 ...
- 【mysql】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;