<?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. Tomcat Nginx cluster note

    nginx install 需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩. 5.安装nginx Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来 ...

  2. Java基础之一组有用的类——使用二叉树搜索算法搜索某个作者(TryBinarySearch)

    控制台程序. Arrays类中的binarySearch()静态方法使用二叉树搜索算法,在有序数组中查找包含给定值的元素.只有当数组的元素按升序方式排序时,该方法才是最有效的,否则就应在调用binar ...

  3. "淘宝推荐系统简介"分享总结

    概述: 此分享是关于淘宝推荐系统简介 1.推荐引擎就是:如何找到用户感兴趣的东西和以什么形式告诉用户:2.推荐引擎的作用:提高用户忠诚度,提高成交转化率和提高网站交叉销售能力:3.推荐系统核心:产品, ...

  4. Java 集合类型

  5. IOS 加载Xib 后 如何 动态修改xib中的控件frame

    看看xib里view是不是设置了自动布局 use auto layout.取消掉就可以了.

  6. 面向对象 理解 C#复习

    面向对象: 是基于万物皆对象这个哲学观点. 所谓的面向对象就是将我们的程序模块化,对象化,把具体事物的特性属性和通过这些属性来实现一些动作的具体方法放到一个类里面 通俗点讲: 一切都是对象 举例: 将 ...

  7. Uva 11538 - Chess Queen

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. CCF真题之相反数

    201403-1 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数). 输入格式 第一行包含一个正整数 N.(1 ≤ N ≤ 500).   ...

  9. CSS_03_04_CSS伪元素选择器

    第01步:编写css代码:wei.css @charset "utf-8"; /* 伪元素选择器 :状态 效果顺序:L V H A */ a:link.lin_01{/*超链接,未 ...

  10. 大数据量的csv文件如何导入到 sql 数据库

    BULK INSERT dbo.T_test001 FROM 'E:\bus_20160316\bus全量评级及借款编号_20160316.csv' WITH ( FIELDTERMINATOR =' ...