运行结果:

<!--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中的验证码类(完善验证码)的更多相关文章

  1. ThinkPHP中使用Verify类生产验证码不显示的原因

    今天在做网站部署的时候,发现登录页面的验证码显示不出来了,而且不报任何错误. 直接通过url访问该操作也不能显示. 后来在网上查找了一些解决方法. 在调用$verify = new \Think\Ve ...

  2. PHP中的验证码类(验证码功能设计之二)

    运行结果: <!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 ...

  3. PHP中的验证码类(验证码功能设计之一)

    <!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 priva ...

  4. CI框架中,扩展验证码类。

    使用CI框架的朋友,应该都知道CI框架的的验证码辅助函数,不太好用.它需要写入到数据库中,然后再进行比对. 大家在实际项目中,好像不会这样去使用,因为会对数据库造成一定的压力. 所以,我们还是利用se ...

  5. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  6. THINKPHP源码学习--------验证码类

    TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...

  7. ThinkPHP 3.2.3 加减乘法验证码类

    ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...

  8. 一个漂亮的php验证码类(分享)

    直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...

  9. PHP编写的图片验证码类文件分享方法

    适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...

  10. laravel加入验证码类几种方法 && Laravel引入第三方库的方法

    1,使用require , inlcude 的方法将验证码类文件包含进来,再进行new 2,将验证码类文件放于Http目录下面,也就是和控制器controller放在一个目录下面,在验证码类文件中加上 ...

随机推荐

  1. 使用Process组件访问本地进程

    实现效果; 知识运用: Process组件的StartInfo属性 //获取或设置要传递给Process的Start方法的属性 public ProcessStartInfo StartInfo {g ...

  2. Problem O: 国家排序

    Problem O: 国家排序 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 405  Solved: 253[Submit][Status][Web ...

  3. Java 从资源文件(.properties)中读取数据

    在Java工程目录src下,创建一个后缀为.properties的文件,例如db.properties 文件中的内容如下(键=值): name=mk age=123 address=China 在程序 ...

  4. Python 消息框的处理

    在实际系统中,在完成某些操作时会弹出对话框来提示,主要分为"警告消息框","确认消息框","提示消息对话"三种类型的对话框. 1.警告消息框 ...

  5. 【期望dp】bzoj4832: [Lydsy1704月赛]抵制克苏恩

    这个题面怎么这么歧义…… Description 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一 ...

  6. 使用jmeter做简单的压测(检查点、负载设置、聚合报告)

    1.添加断言(检查点) 在需要压测的接口下添加--断言--响应断言,取接口响应中包含有的数据即可 检查点HTTP请求-->断言-->响应断言1.名称.注释2.Apply to//作用于哪里 ...

  7. CentOS 7 忘记root密码解决方法

    CentOS 7  root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例: 1.重启机器,进入grub菜单的时候按e ...

  8. vue之列表循环

    文档:https://cn.vuejs.org/v2/guide/list.html 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用“就地复用”策略.如果数据项的顺序被改变, ...

  9. hdu 5441

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  10. poj 2229 拆数问题 dp算法

    题意:一个n可以拆成 2的幂的和有多少种 思路:先看实例 1   1 2    1+1     2 3     1+1+1  1+2 4      1+1+1+1  1+1+2  2+2  4 5  ...