<?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. 解决td标签上的position:relative属性在各浏览器中的兼容性问题

    在css中的position属性规定了页面元素的定位类型,它有以下几个值: absolute:绝对定位,相对于static以外的第一个父元素进行定位: fixed:生成绝对定位的元素,相对于浏览器窗口 ...

  2. SQL Server中使用正则表达式

    SQL Server 2005及以上版本支持用CLR语言(C# .NET.VB.NET)编写过程.触发器和函数,因此使得正则匹配,数据提取能够在SQL中灵活运用,大大提高了SQL处理字符串,文本等内容 ...

  3. CSS之CSS hack

    由于不同的浏览器对CSS的支持及解析结果不一样,还由于CSS中的优先级的关系.我们就可以根据这个来针对不同的浏览器来写不同的CSS. CSS Hack大致有3种表现形式,CSS类内部Hack.选择器H ...

  4. poj: 3006

    简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...

  5. 浅谈thinkphp中将字符串转换成json数组的方法

    这是一部分代码: $client = M("Client");$data = $client->where('user_id ='.$user_id)->select( ...

  6. StringBuffer类总结

    package day13; /* StringBuffer是字符串缓冲区. 是一个容器. 特点: 1,长度是可变化的. 2,可以字节操作多个数据类型. 3,最终会通过toString方法变成字符串. ...

  7. hdu5255 魔法因子

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5255 首先先预处理出一个p,使得p*因子X等于一个整数,且p最小,设q=p*X. 则题目则可以看成存在 ...

  8. jvm排查工具

    jps jps -mvl --查看java的ps进程. jstack 打印一个线程堆栈信息 top -H -p pid1 -> 得到占用资源大的pid2 jstack pid1 | grep & ...

  9. MYSQL日期类型的加减更新使用INTERVAL 1 DAY

    例如:UPDATE teachingplan SET teachPlanBeginTime = teachPlanBeginTime +INTERVAL 1 DAY

  10. RobotFrameWork接口报文测试-----(二)demo的升级版

    在上一篇,简单的demo实现了讲xml的数据发送服务器端并取得recvi_buf,然后进行了简单的解析的操作.现在就要解决之前提过的2个问题: 1. 步骤这么多,难道每写一个脚本都要重复一次么? 2. ...