<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/6/20
* Time: 14:29
*/ Class captcha{
//验证码类
protected $str="xaaxqwe556232assd"; //随机数
protected $code; //验证码
protected $length=4; //验证码长度
protected $width=80; //验证码宽度
protected $height=30; //验证码高度
protected $img;//验证码生成 //随机数
public function getcode()
{
$len=strlen($this->str)-1;
for($i=0;$i<4;$i++)
{
$this->code.=$this->str[mt_rand(0,$len)];
} } //生成背景
public function codeBg()
{
//新建一个图像
$this->img=imagecreatetruecolor($this->width,$this->height);
$color=imagecolorallocate($this->img,rand(1,100),rand(1,100),rand(1,100)); //红,绿,蓝
//$back=imagecolorallocate($this->img,0,0,0);
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color); } //生成干扰元素 public function setLine()
{
//干扰点
for($i=0;$i<1000;$i++)
{
$color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->img,rand(1,99),rand(1,99),$color);
}
//
// //干扰线
for($i=0;$i<5;$i++)
{
$color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
imageline($this->img,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),$color);
}
//
// //干扰线 } //生成元素头
public function outHeader()
{
header("Content-type:image/png");
} //写入验证码 public function writeString()
{
$red = imagecolorallocate ( $this->img , 255 , 0 , 0 );
imagestring($this->img,5,rand(1,15),rand(1,15),$this->code(),$red);
//$font = 'arial.ttf' ;
//imagettftext ( $this->img , 20 , 0 , 10 , 20 , $red , $font , $this->code );
}
//shu public function png()
{
$this->outHeader();
$this->codeBg();
$this->getcode();
$this->setLine(); $this->writeString();
session_start();
$_SESSION['code']=$this->code();
imagepng($this->img);
}
public function code(){
return strtoupper($this->code);
} } $ce=new captcha();
$ce->png();
var_dump($_SESSION['code']);

  测试页面

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<?php session_start(); ?>
<body>
<img title="点击刷新" src="./captcha.class.php" onclick="this.src='captcha.class.php?'+Math.random();"/>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name='captcha' />
<input type="submit" name='submit' value="buton" />
</form>
</body>
</html> <?php
if($_POST['submit']){
if($_POST['captcha']!==$_SESSION['code'])
{
echo "登录失败";
}else{
echo "成功";
}
} ?>

  

PHP之验证码类的更多相关文章

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

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

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

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

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

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

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

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

  5. 【个人使用.Net类库】(4)验证码类

    验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...

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

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

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

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

  8. 一个好用的PHP验证码类

    分享一个好用的php验证码类,包括调用示例. 说明: 如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用到imagettftext()函数.字体的位置在C盘下 ...

  9. 简单实用的PHP验证码类

    一个简单实用的php验证码类,分享出来 ,供大家参考. 代码如下: <?php /** @ php 验证码类 @ http://www.jbxue.com */ Class code { var ...

随机推荐

  1. 单位px 转换成 rem

    <script type="text/javascript"> var oHtml = document.documentElement; getSize(); win ...

  2. Java 集合类型

  3. 在Adobe AIR/AS 程序中 如何设置目录

    首先所有目录都以 "File:///"开头,无论Mac或者Windows 后面的路径 Windows: E:/WorkGround/Txt.txt  --> "Fi ...

  4. JSP 使用框架frame

    JSP使用框架放在<head>标签里面.如果放在<body>标签里面,用Tomcat打开,是不会显示的.

  5. 01分数规划zoj2676(最优比例,最小割集+二分)

    ZOJ Problem Set - 2676         Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      S ...

  6. 如何使用highmaps制作中国地图

    如何使用highmaps制作中国地图 文章目录 Highmaps 所需文件 地图初始化代码 highmaps 渲染讲解 highmaps 中国各城市坐标的json文件 highmaps 线上DEMO ...

  7. jquery遍历对象,数组,集合

    1.jquery 遍历对象 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTM ...

  8. 夺命雷公狗---微信开发51----网页授权(oauth2.0)获取用户基本信息接口(1)

    如果用户在微信客户端访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,从而实现业务逻辑. 一般我们用来“数据采集”,“市场调查”,“投票”,只要授权了第三方网页,微信用户无需注册就可 ...

  9. linux添加字体的过程

    只说一下过程, 至于具体的原理还没搞明白. 1. 首先你要有字体文件,ttf或者ttc格式的均可以 我们可以从windows的 C:\WINDOWS\Fonts\ 这个目录下的字体文件复制出来,例如我 ...

  10. 在linux中的virtualbox无法挂载usb设备的解决方法

    方法来源于网络. 在安装完virtualbox之后,virtualbox会建立一个名为 vboxusers 的组,将你的用户名加入到该组即可. 命令参考: #usermod -a -G vboxuse ...