这里不多说,直接上代码!

/** 
* 改造的加减法验证类
* 使用示例 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 算式验证码的更多相关文章

  1. PHP算式验证码和汉字验证码的实现方法

    在PHP网站开发中,验证码可以有效地保护我们的表单不被恶意提交,但是如果不使用算式验证码或者汉字验证码,仅仅使用简单的字母或者数字验证码,这样的验证码方案真的安全吗? 大家知道简单数字或者字母验证码很 ...

  2. jsp实现验证码

    在web开发领域里面,验证码是一个比较常见的功能,而归根到底,验证码其实就是一组随机数,或者是一个随机算术 一.基本知识 1.为什么需要验证码? 验证码,很多时候出现在注册页面或者登陆界面,在这些页面 ...

  3. Java生成验证码(二)

    前一篇博客已经介绍了如何用Java servlet产生验证码,本篇继续介绍如何使用一些开源组件生成验证码 ————————————————————————————————————————————   ...

  4. Java实现验证码的制作

    验证码概述 为什么使用验证码? 验证码(CAPTCHA)是一种全自动程序.主要是为了区分“进行操作的是不是人”.如果没有验证码机制,将会导致以下的问题: 对特定网站不断进行登录,破解密码: 对某个网站 ...

  5. 构建一个.net的干货类库,以便于快速的开发 - 验证码

    一个验证码对于一个网站的作用不言而喻,而随着技术的发展验证码的种类也开始多了起来,发展至今很多网站已经不再使用一种验证码,为满足需求程序猿就要写很多的方法来适应需求,今天我就来介绍我之前收集到的验证码 ...

  6. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  7. Delphi识别读取验证码

    unit OCR; interface uses Windows, SysUtils, Graphics, Classes, PNGImage, GIFImage, JPEG, Math, Asphy ...

  8. .net点选验证码实现思路分享

    哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...

  9. 【探索】无形验证码 —— PoW 算力验证

    先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...

随机推荐

  1. Linux筛选数据

    grep命令:在文件中查找关键字,并显示所在行(Globally search a Regular Expression and Print) grep text file:在file文件中搜索tex ...

  2. 解决chrome浏览器安装不上的问题

    1.  打开注册表:  windows键 + R --> 输入regedit -->  回车 (注:windows键在左ctrl附近微软图标的键) 2.  找到 32位:HKEY_LOCA ...

  3. Linux设置数据库自动备份

    本文为转载,最末端为原地址 以CentOS 7.6系统与Oracle 11g为例: 一.先找到数据库的环境变量 如果是在root账户下,须先登录到数据库所在账户 su oraclecat ~/.bas ...

  4. 批量定时任务将rtf文件转为docx,入参是rtf文件夹,生成一个docx文件夹

    java,python等语言对于rft的处理很受限,rtf提供了很少的api供外部调用处理,但是对于docx我们却又很多api来处理,所以很多人会有需求将rtf批量转为docx的需求,接下来就来说说解 ...

  5. The main Method

    The main Method You can call static methods without having any objects. For example, you never const ...

  6. boost number handling

    Boost.Integer defines specialized for integers. 1. types for integers with number of bits #include & ...

  7. php str_repeat()函数 语法

    php str_repeat()函数 语法 str_repeat()函数怎么用? php str_repeat()函数用于重复使用指定字符串,语法是str_repeat(string,repeat), ...

  8. JS中的作用域及闭包

    1.JS中的作用域 在 es6 出现之前JS中只有全局作用域和函数作用域,没有块级作用域,即 JS 在函数体内有自己的作用域,但是如果不是在函数体的话就全部都是全局作用域.比如在 if.for 等有 ...

  9. Codeforces 831C--Jury Marks (思维)

    题目链接:http://codeforces.com/problemset/problem/831/C 题意:有一位参赛选手,我们不知道他初始或最后的成绩,但是知道k次评审所加(减)的分数,以及n个在 ...

  10. Bugku | 游戏过关

    思路:绕过判断,直接跳转到算flag的函数哪里 1.找到计算flag的函数在哪里,记住 "0075e940",这是入口 2.找到一个现成的跳转指令,修改它: 3.重新运行一遍,得到 ...