获取验证码随机字符串@return string $captcha,随机验证码文字
<?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,随机验证码文字的更多相关文章
- PHP学习笔记:万能随机字符串生成函数(已经封装好)
做验证码用到的,然后就把这个函数封装起来,使用时候要设置2个参数: $str设置里要被采集的字符串,比如: $str='efasfgzsrhftjxjxjhsrth'; 则在函数里面生成的字符串就回从 ...
- 【代码笔记】iOS-产生随机字符串
一,代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...
- java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串
package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...
- shell 生成指定范围随机数与随机字符串 .
shell 生成指定范围随机数与随机字符串 分类: shell 2014-04-22 22:17 20902人阅读 评 ...
- 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 ...
- php生成随机字符串和验证码的类
网上有很多的php随机数与验证码的代码与文章,真正适用的没有几个. 索性自己搞一个吧. 开始本节的php教程 吧,以下代码的实现,主要做到可以很好区分一个get_code(),另一个create_ch ...
- go golang 判断base64数据 获取随机字符串 截取字符串
go golang 判断base64数据 获取随机字符串 截取字符串 先少写点,占个坑,以后接着加. 1,获取指定长度随机字符串 func RandomDigits(length int) strin ...
- js随机生成一个数组中的随机字符串以及更新验证码
随机生成m,n范围的值得公式: Math.random()*(n-m)+m: 改正公式:Math.random()*(n+1-m)+m // 生成随机字符串function randomMixed(n ...
- php获取随机字符串的几种方法
方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) /** * 获得随机字符串 * @param $len 需要的长度 * @param $special ...
随机推荐
- 配置nginx.config 报错:connect() failed (111: Connection refused) while connecting to upstream解决
php-fpm没有运行 执行如下命令查看是否启动了php-fpm,如果没有则启动你的php-fpm即可 查看: netstat -ant | grep 9000 执行 /usr/local/php/s ...
- liunx 时间ntp同步服务器
1.root 用户下安装 yum install ntp -y 报错如下: 29 Apr 00:25:04 ntpdate[8786]: the NTP socket is in use, exiti ...
- java的子类覆盖梗
项目上线,用户注册时验证码一直报错误,数据库也没问题,代码貌似也没问题. 后面排查到最后,发现是一个子类覆盖父属性问题. JAVA代码中,子类覆盖父类的私有.保护属性,如果不设置get.set方法,拿 ...
- Qt打包部署程序自动查找依赖DLL工具windeployqt
qt编译好一个exe程序之后,部署到一台没有开发环境的机器上,需要一起拷贝其依赖的dll文件.这时需要一个windeployqt工具来帮忙,因为手动拷贝的话容易遗漏. https://blog.csd ...
- debug-stripped.ap_' specified for property 'resourceFile' does not exist
1.关闭 Instant Run 2. 关闭混淆(混淆的问题) buildTypes { release { minifyEnabled true shrinkResources true progu ...
- 《利用Python 进行数据分析》 - 笔记(4)----json
解决方案: 读写文本格式的数据: pandas 提供了一些用于将表格型数据读取为DataFrame对象的函数 pandas 中的解析函数 函数的选项可以划分为以下几个大类 索引:将一个或多个列当做返回 ...
- 敏感词过滤和XML的创建
今天我慢下来啦,因为这三天没有新的课程学习内容,自己仅仅看啦一些,这让我停下来栖息片刻:说说现在的生活,简单的进行着,每天要奔波着去上课,然后回来,每天都在想怎样学习这个小知识点,大脑也在想怎样解决程 ...
- setcursor 与 showcursor
Windows为鼠标光标保存了一个「显示计数」.如果安装了鼠标,显示计数会被初始化为0:否则,显示计数会被初始化为-1. 只有在显示计数非负时才显示鼠标光标.要增加显示计数,呼叫:ShowCursor ...
- 设置ADB网络连接目标板
adb connect 网络连接目标板报错,原因参考[http://ytydyd.blog.sohu.com/146260552.html].而且指定 adb connect <IP>:5 ...
- Python学习(22):模块
转自 http://www.cnblogs.com/BeginMan/p/3183656.html 一.模块基础 1.模块 自我包含,且有组织的代码片段就是模块 模块是Pyhon最高级别的程序组织单元 ...