<?php
//验证码工具类
class Captcha{
//属性
private $width;
private $height;
private $fontsize;
private $pixes;
private $lines;
private $str_len;
/*
* 构造方法
* @param1 array $arr = array(),初始化属性的关联数组
*/
public function __construct($arr = array()){
//初始化
$this->width = isset($arr['width']) ? $arr['width'] : $GLOBALS['config']['captcha']['width'];
$this->height = isset($arr['height']) ? $arr['height'] : $GLOBALS['config']['captcha']['height'];
$this->fontsize = isset($arr['fontsize']) ? $arr['fontsize'] : $GLOBALS['config']['captcha']['fontsize'];
$this->pixes = isset($arr['pixes']) ? $arr['pixes'] : $GLOBALS['config']['captcha']['pixes'];
$this->lines = isset($arr['lines']) ? $arr['lines'] : $GLOBALS['config']['captcha']['lines'];
$this->str_len = isset($arr['str_len']) ? $arr['str_len'] : $GLOBALS['config']['captcha']['str_len'];
}
/*
* 产生验证码图片
*/
public function generate(){
//制作画布
$img = imagecreatetruecolor($this->width,$this->height);
//给定背景色
$bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagefill($img,0,0,$bg_color);
//制作干扰线
$this->getLines($img);
//增加干扰点
$this->getPixels($img);
//增加验证码文字
$captcha = $this->getCaptcha();
//文字颜色
$str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
//写入文字
//计算文字应该出现的起始位置
$start_x = ceil($this->width/2) - 25;
$start_y = ceil($this->height/2) - 2881064151;
if(imagestring($img,$this->fontsize,$start_x,$start_y,$captcha,$str_color)){
//成功:输出验证码
header('Content-type:image/png');
imagepng($img);
}else{
//失败
return false;
}
}
/*
* 获取验证码随机字符串
* @return string $captcha,随机验证码文字
*/
private function getCaptcha(){
//获取随机字符串
$str = implode('',array_merge(range('a','z'),range('A','Z'),range(1,9)));
//随机取
$captcha = ''; //保存随机字符串
for($i = 0,$len = strlen($str);$i < $this->str_len;$i++){
//每次随机取一个字符
$captcha .= $str[mt_rand(0,$len - 1)] . ' ';
}
//将数据保存到session
$_SESSION['captcha'] = str_replace(' ','',$captcha);
//返回值
return $captcha;
}
/*
* 增加干扰点
* @param1 resource $img
*/
private function getPixels($img){
//增加干扰点
for($i = 0;$i < $this->pixes;$i++){
//分配颜色
$pixel_color = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
//画点
imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color);
}
}
/*
* 增加干扰线
* @param1 resource $img,要增加干扰线的图片资源
*/
private function getLines($img){
//增加干扰线
for($i = 0;$i < $this->lines;$i++){
//分配颜色
$line_color = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
//画线
imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color);
}
}
/*
* 验证验证码
* @param1 string $captcha,用户提交的验证码
* @return bool,成功返回true,失败返回false
*/
public static function checkCaptcha($captcha){
//验证码不区分大小写
return (strtolower($captcha) === strtolower($_SESSION['captcha']));
}
}

获取验证码随机字符串@return string $captcha,随机验证码文字的更多相关文章

  1. PHP学习笔记:万能随机字符串生成函数(已经封装好)

    做验证码用到的,然后就把这个函数封装起来,使用时候要设置2个参数: $str设置里要被采集的字符串,比如: $str='efasfgzsrhftjxjxjhsrth'; 则在函数里面生成的字符串就回从 ...

  2. 【代码笔记】iOS-产生随机字符串

    一,代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...

  3. java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串

    package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...

  4. shell 生成指定范围随机数与随机字符串 .

    shell 生成指定范围随机数与随机字符串         分类:             shell              2014-04-22 22:17     20902人阅读     评 ...

  5. random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串

    openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...

  6. php生成随机字符串和验证码的类

    网上有很多的php随机数与验证码的代码与文章,真正适用的没有几个. 索性自己搞一个吧. 开始本节的php教程 吧,以下代码的实现,主要做到可以很好区分一个get_code(),另一个create_ch ...

  7. go golang 判断base64数据 获取随机字符串 截取字符串

    go golang 判断base64数据 获取随机字符串 截取字符串 先少写点,占个坑,以后接着加. 1,获取指定长度随机字符串 func RandomDigits(length int) strin ...

  8. js随机生成一个数组中的随机字符串以及更新验证码

    随机生成m,n范围的值得公式: Math.random()*(n-m)+m: 改正公式:Math.random()*(n+1-m)+m // 生成随机字符串function randomMixed(n ...

  9. php获取随机字符串的几种方法

    方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) /** * 获得随机字符串 * @param $len 需要的长度 * @param $special ...

随机推荐

  1. Linux安装bundle

    安装bundle文件的方法: cd 到文件目录再用sudo chmod +x XXXXXXX.bundle 加权限最后 ./XXXXXXXX.bundle 就行了. 第一步:sudo chmod +x ...

  2. 织梦Dedecms系统可疑文件include/filter.inc.php扫描出漏洞,该如何解决?

    今天在做网站监察的时候,发现网站出了一个问题,在对网站做木马监测的时候,扫描出一个可疑文件:/include/filter.inc.php,建议删除,但仔细检查后,发现此文件是织梦(Dedecms)系 ...

  3. str_replace使用

    $begintime=str_replace("-", "","2014-03-05");        (需要替换的字符串,换成字符串,需 ...

  4. redis队列操作

    PHP版: <?php /** * Redis * 配置 $redis_host,$redis_port * 队列操作 * @author win 7 */ class RQueue{ priv ...

  5. c++Valgrind内存检测工具---19

    原创博文,转载请标明出处--周学伟  http://www.cnblogs.com/zxouxuewei/ 一.Valgrind 概述 Valgrind是一套Linux下,开放源代码(GPL V2)的 ...

  6. Nexus5 电信3G保留数据和Root升级Android 6.0

    前提: A 备份手机重要数据,安全第一 B 进入twrp recovery 备份EFS,建议最好拷贝到电脑上(如果没有twrp,则需要先刷twrp,具体指令请看下面步骤第10条) C 因为Androi ...

  7. 【代码审计】iZhanCMS_v2.1 代码执行漏洞分析

      0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...

  8. Linux应急响应入侵排查思路

    0x00 前言 ​ 当企业发生黑客入侵.系统崩溃或其它影响业务正常运行的安全事件时,急需第一时间进行处理,使企业的网络信息系统在最短时间内恢复正常工作,进一步查找入侵来源,还原入侵事故过程,同时给出解 ...

  9. jquery 动态展示查询条件

    <table class="queryTable" width="100%" > <tr> <td class="que ...

  10. 敏捷开发中的Scrum流程和术语【转】

    任何人力流程都离不开人来执行,所以在讲解Scrum流程之前,有必要先把Scrum中的角色讲一下. 一天,一头猪和一只鸡在路上散步,鸡看了一下猪说,“嗨,我们合伙开一家餐馆怎么样?”,猪回头看了一下鸡说 ...