PHP之验证码类
<?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之验证码类的更多相关文章
- 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 ...
- 【个人使用.Net类库】(4)验证码类
验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...
- PHP编写的图片验证码类文件分享方法
适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...
- laravel加入验证码类几种方法 && Laravel引入第三方库的方法
1,使用require , inlcude 的方法将验证码类文件包含进来,再进行new 2,将验证码类文件放于Http目录下面,也就是和控制器controller放在一个目录下面,在验证码类文件中加上 ...
- 一个好用的PHP验证码类
分享一个好用的php验证码类,包括调用示例. 说明: 如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用到imagettftext()函数.字体的位置在C盘下 ...
- 简单实用的PHP验证码类
一个简单实用的php验证码类,分享出来 ,供大家参考. 代码如下: <?php /** @ php 验证码类 @ http://www.jbxue.com */ Class code { var ...
随机推荐
- C#资源文件和C#枚举如何结合使用?
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...
- Oracle官方文档在线查看
1.9i Oracle官方文档在线查看 http://www.oracle.com/pls/db92/homepage 2.10g Oracle官方文档线查看 http://www.oracle.co ...
- [Intellij IDEA]File size exceeds configured limit(2560000). Code insight features are not available
在使用 IDEA, 发现一个问题File size exceeds configured limit (2560000). Code insight features not available.
- PostgreSQL9.1 upgrade to PostgreSQL9.5rc1
PostgreSQL9.1.0 upgrade to PostgreSQL9.5rc1 安装PG9.1端口为5432 [pgup@minion1 pg]$ ls postgresql-9.1.0.ta ...
- 0330 复利程序c语言版转java版 会逐渐更进版
import java.util.Scanner; public class compounding { public static void main(String[] args) { menu() ...
- 去掉字符串中的空格 JS JQ 正则三种不同写法
<script> function trim(str) { return str.replace(/(^\s*|\s*$)/g, "") } console.log(t ...
- Python学习总结11:获取当前运行类名和函数名
一. 使用内置方法和修饰器方法获取类名.函数名 1. 外部获取 从外部的情况好获取,可以使用指向函数的对象,然后用__name__属性. def a(): pass a.__name__ 或者 get ...
- javascript 异常处理和事件处理
异常捕获 1.异常:当javascript引擎执行JS代码时,发生了错误,导致程序停止运行. 2.异常抛出:当异常产生,并且将这个异常生成一个错误信息 3.异常捕获: try{发生异常的代码块:}ca ...
- 父类构造函数中的this指针在子类构造对象后,这个this指针指向什么
子类的对象内存布局包括两部分:父类和子类派生部分,所以执行父类的构造函数只不过是在构造子类对象的父类部分.因此子类对象的this指针是指向子类对象自己.
- Android 利用Service实现下载网络图片至sdk卡
package com.example.myapp5; import android.app.Activity; import android.content.Intent; import andro ...