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.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...
随机推荐
- 75.Binary Tree Maximum Path Sum(二叉树的最大路径和)
Level: Hard 题目描述: Given a non-empty binary tree, find the maximum path sum. For this problem, a pa ...
- C# 下载PDF文件(http与ftp)
1.下载http模式的pdf文件(以ASP.NET为例,将PDF存在项目的目录下,可以通过http直接打开项目下的pdf文件) #region 调用本地文件使用返回pdfbyte数组 /// < ...
- 2018-8-10-win10-uwp-毛玻璃
title author date CreateTime categories win10 uwp 毛玻璃 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 17 ...
- 二、IDS4配置服务
它是根据定义配置服务Config.cs文件来生成客户端和API使用该服务所需的配置数据. 一.IDS4签名服务 1.为项目添加NuGet包. 2.IDS4服务制定的配置Config.cs. using ...
- Codeforces Round #454 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)
题目链接:http://codeforces.com/contest/906/problem/D 题目大意:给定n个整数w[1],w[2],……,w[n],和一个数m,然后有q个询问,每个询问给出一个 ...
- 第二节:专做自己是小白——重新认识MySQL 学习记录
一.安装MySQL的一些知识点 1.进程号是操作系统随机分配,每次启动程序都会有一个新的进程号. 2.mysql服务器进程默认名称MySQL,MySQL客户端进程默认名称mysql. 3. ...
- CentOS 7.4 安装python3及虚拟环境
[转]:https://www.centos.bz/2018/05/centos-7-4-%e5%ae%89%e8%a3%85python3%e5%8f%8a%e8%99%9a%e6%8b%9f%e7 ...
- This MySqlConnection is already in use
项目中类似于以下的代码,实际的代码要更复杂,DbContext是依赖注入的: 报错如下: This MySqlConnection is already in use. See https://fl. ...
- poj 3468: A Simple Problem with Integers (树状数组区间更新)
题目链接: http://poj.org/problem?id=3468 题目是对一个数组,支持两种操作 操作C:对下标从a到b的每个元素,值增加c: 操作Q:对求下标从a到b的元素值之和. 这道题也 ...
- pycharm 进入Pythonshell脚本调试