PHP 算式验证码
这里不多说,直接上代码!
/**
* 改造的加减法验证类
* 使用示例 VerifyCode::get('xxx', 20);
* 验证示例 VerifyCode::check('1', 'xxx');
*/
class VerifyCode
{
/**
* php验证码 算式验证码
*/
public static function get($one,$two,$prefix = '', $font_size = 28)
{
//文件头...
header("Content-type: image/png");
ob_end_clean();
//创建真彩色白纸
$width = $font_size*5;
$height = $font_size+1;
$im = @imagecreatetruecolor($width, $height) or die("建立图像失败");
//获取背景颜色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景颜色
imagefill($im, 0, 0, $background_color);
//获取边框颜色
$border_color = imagecolorallocate($im, 200, 200, 200);
//画矩形,边框颜色200,200,200
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color); //逐行炫耀背景,全屏用1或0
for($i = 2;$i < $height - 2;$i++) {
//获取随机淡色
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
//画线
//imageline($im, 2, $i, $width - 1, $i, $line_color); //画一条线 画线条
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $line_color); //画椭圆
} //设置印上去的文字
$firstNum = $one;
$secondNum = $two; /*
* 随机获取值来判断是乘除还是加减
*/ if($one > $two){
$chu_num = $one/$two;
}
$rand_num = mt_rand(1,5);
if($rand_num != 1 && is_int($chu_num) && $two != 0){ //判断结果是否为整数
$actionStr = $firstNum > $secondNum ? '/' : '*';
}else{
$actionStr = $firstNum > $secondNum ? '-' : '+';
} //获取第1个随机文字
$imstr[0]["s"] = $firstNum;
$imstr[0]["x"] = rand(2, 5);
$imstr[0]["y"] = rand(1, 4); //获取第2个随机文字
$imstr[1]["s"] = $actionStr;
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
$imstr[1]["y"] = rand(1,5); //获取第3个随机文字
$imstr[2]["s"] = $secondNum;
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
$imstr[2]["y"] = rand(1, 5); //获取第3个随机文字
$imstr[3]["s"] = '=';
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
$imstr[3]["y"] = 3; //获取第3个随机文字
$imstr[4]["s"] = '?';
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
$imstr[4]["y"] = 3; //文字
$text = '';
//获取随机较深颜色
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180)); //写入随机字串
for($i = 0; $i < 5; $i++) {
//$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));//获取随机较深颜色
$text .= $imstr[$i]["s"];
//imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);//画文字,设置文字大小
} /*
* 为图片添加噪点,线条,雪花,增加干扰度
* */
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($im,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($im,mt_rand(0,140),mt_rand(0,28),mt_rand(0,140),mt_rand(0,28),$color);
}
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($im,mt_rand(1,5),mt_rand(0,140),mt_rand(0,28),'*',$color);
}
imagestring($im, 5, rand(5,15), rand(0,5), $text, $text_color); if($rand_num != 1 && is_int($chu_num)){
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum / $secondNum) : ($firstNum * $secondNum);
}else{
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);
} //显示图片
imagepng($im);
//销毁图片
imagedestroy($im); }
public static function check($code, $prefix = '')
{
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {
return true;
} else {
return false;
}
}
}
PHP 算式验证码的更多相关文章
- PHP算式验证码和汉字验证码的实现方法
在PHP网站开发中,验证码可以有效地保护我们的表单不被恶意提交,但是如果不使用算式验证码或者汉字验证码,仅仅使用简单的字母或者数字验证码,这样的验证码方案真的安全吗? 大家知道简单数字或者字母验证码很 ...
- jsp实现验证码
在web开发领域里面,验证码是一个比较常见的功能,而归根到底,验证码其实就是一组随机数,或者是一个随机算术 一.基本知识 1.为什么需要验证码? 验证码,很多时候出现在注册页面或者登陆界面,在这些页面 ...
- Java生成验证码(二)
前一篇博客已经介绍了如何用Java servlet产生验证码,本篇继续介绍如何使用一些开源组件生成验证码 ———————————————————————————————————————————— ...
- Java实现验证码的制作
验证码概述 为什么使用验证码? 验证码(CAPTCHA)是一种全自动程序.主要是为了区分“进行操作的是不是人”.如果没有验证码机制,将会导致以下的问题: 对特定网站不断进行登录,破解密码: 对某个网站 ...
- 构建一个.net的干货类库,以便于快速的开发 - 验证码
一个验证码对于一个网站的作用不言而喻,而随着技术的发展验证码的种类也开始多了起来,发展至今很多网站已经不再使用一种验证码,为满足需求程序猿就要写很多的方法来适应需求,今天我就来介绍我之前收集到的验证码 ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- Delphi识别读取验证码
unit OCR; interface uses Windows, SysUtils, Graphics, Classes, PNGImage, GIFImage, JPEG, Math, Asphy ...
- .net点选验证码实现思路分享
哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...
- 【探索】无形验证码 —— PoW 算力验证
先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...
随机推荐
- WEB应用安全解决方案测试验证
WEB应用安全解决方案测试报告 --- By jiang.jx at 2017-08-11 WEB应用安全解决方案.docx 链接:https://share.weiyun.com/068b05467 ...
- elasticsearch 深入 —— 相关度控制
控制相关度 处理结构化数据(比如:时间.数字.字符串.枚举)的数据库, 只需检查文档(或关系数据库里的行)是否与查询匹配. 布尔的是/非匹配是全文搜索的基础,但不止如此,我们还要知道每个文档与查询的相 ...
- 使用WakeLock将Android应用程序保持后台唤醒
前言: 一些手机app(如微信.QQ等)有新消息来到达,手机屏幕即使在锁屏状态下也会亮起,并提示用户有新消息.但是,一般情况下手机锁屏后,Android系统为了省电以及减少CPU消耗,在一段时间后会使 ...
- java写文件UTF-8格式
String fileName = dir + File.separator + date + File.separator + (file.list().length + 1) + ".t ...
- Linux学习笔记之磁盘与文件系统的管理
三.Linux磁盘与文件系统的管理 MBR扇区(512B) 磁盘的分区组成 Boot sector 扇区(用来装引导程序) Super block 记录inode与Block的信息 Inod ...
- 微信小程序(12)--倒计时
记录一个常见的效果,倒计时. <text>倒计时:{{content}}</text> Page({ /** * 页面的初始数据 */ data: { endTime: '', ...
- python发行包 IDE
https://blog.csdn.net/qq_38188725/article/details/80624004 https://blog.csdn.net/qq_38188725/article ...
- springboot打包成jar文件无法正常运行,解决办法已经找到
1.用intellij idea 创建了一个springboot的项目,前期都运行的好好的,在ide中可以正常运行,但是打包成Jar运行却一直报错. 2.经过不懈探索,终于找到解决办法 3.首先,找到 ...
- Elastic Search快速入门
https://blog.csdn.net/weixin_42633131/article/details/82902812 通过这个篇文章可以快速入门,快速搭建一个elastic search de ...
- WPF 几种常用控件样式的总结
这里把wpf中几种常用样式总结一下,后期可以直接拷贝使用,呵呵 一.Button <ResourceDictionary xmlns="http://schemas.microsoft ...