php实现算术验证码功能


Posted in PHP onDecember 05, 2018

最近学了php的图像函数,也练习的验证码的实现,在听从老师的建议下,自己写了一个算术验证码的函数,第一次自己独立完成一个小练习,感觉挺开心的。但是,也知道自己的局限,所以特意写出来,希望各路大神指点一下,能够更多的拓展自己的视野。

php代码如下:实现验证码的功能

<?php
/**
 * @param int $width 宽度,默认为120
 * @param int $height 高度,默认为50
 * @param int $fontSize 字体的大小
 * @return 图片资源
 */
function arithmeticCode($width=120,$height=50,$fontSize=20){
  //开启session
  session_start();
  //创建画布
  $img = imagecreatetruecolor($width,$height);
  //分配颜色
  $color = imagecolorallocate($img,255,255,255);
  //填充颜色
  imagefill($img,0,0,$color);

  //干扰点
  for ($i = 0;$i < 500;$i++){
    $pixColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$pixColor);
  }
  //干扰线
  for ($i = 0;$i < 4;$i++){
    $lineColor = imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
    imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$lineColor);
  }

  //定义一个数组存放运算符号
  $arr = ['+','-','*'];
  //计算数组的长度
  $len = count($arr);
  //定义一个1到20的数组
  $num = range(1,20);
  $numLen = count($num);
  //定义一个空数组来存放随机取得的验证码
  $code = [];
  for ($i = 0;$i < $len;$i++) {
    if ($i == 1) {
      $code[] = $arr[mt_rand(0,$len-1)];
    }else {
      $code[] = $num[mt_rand(0,$numLen-1)];
    }
  }

  $str = implode($code);//将数组转为字符串
  $textColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
  $fontAngle = 0;
  $x = ($width - $fontSize*3)/2;
  $y = ($height - $fontSize) / 2 + $fontSize;
  imagettftext($img,$fontSize,$fontAngle,$x,$y,$textColor,"./img/msyh.ttc",$str);

  $res = getRes($code);


  //将函数存放在session中
  $_SESSION['res'] = $res;

  //输出图片
  header("content-type:image/png");
  imagepng($img);
}


/**
 * @param $arr 一个包含运算符号的数组
 * @return 返回一个运算结果
 */
function getRes($arr) {
  $sum = 0;
  //判断数组元素下标为1的运算符号是什么
  switch ($arr[1]){
    case '+':
      $sum = $arr[0] + $arr[2];
      break;
    case '-':
      $sum = $arr[0] - $arr[2];
      break;
    case '*':
      $sum = $arr[0] * $arr[2];
      break;
  }

  return $sum;
}

//调用函数
arithmeticCode(100,40,18);

html的部分代码

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
     content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    img {
      position: relative;
      top: 20px;
    }
  </style>
</head>
<body>
<form action="test.php">
  验证码 <input type="text" name="code">
  <img src="./demo5.php" alt="点击刷新">
  <br>
  <button>提交</button>
</form>
</body>
</html>
<script>
  //实现点击图片刷新验证码的功能
  var img = document.querySelector("img");
  img.onclick = function () {
    this.src = this.src+"?m="+Math.random();
  }
</script>

test.php的测试

<?php
session_start();
$res = $_SESSION['res'];
$value = $_GET['code'];
if ($res == $value) {
  echo "test success";
}else{
  echo "test fail";
}

以上就是我的代码组成.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
php中文字符截取防乱码
Mar 28 PHP
PHP 存储文本换行实现方法
Jan 05 PHP
php printf输出格式使用说明
Dec 05 PHP
php构造函数实例讲解
Nov 13 PHP
php利用cookies实现购物车的方法
Dec 10 PHP
php自定义加密与解密程序实例
Dec 31 PHP
PHP连接数据库实现注册页面的增删改查操作
Mar 27 PHP
PHP仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(上)
May 26 PHP
PHP文字转图片功能原理与实现方法分析
Aug 31 PHP
PHP实现UTF8二进制及明文字符串的转化功能示例
Nov 20 PHP
如何在centos8自定义目录安装php7.3
Nov 28 PHP
PHP并发场景的三种解决方案代码实例
Feb 27 PHP
php使用array_chunk函数将一个数组分割成多个数组
Dec 05 #PHP
Yii支持多域名cors原理的实现
Dec 05 #PHP
php判断电子邮件是否正确方法
Dec 04 #PHP
浅谈Laravel核心解读之Console内核
Dec 02 #PHP
Laravel使用scout集成elasticsearch做全文搜索的实现方法
Nov 30 #PHP
用Laravel Sms实现laravel短信验证码的发送的实现
Nov 29 #PHP
php实现每日签到功能
Nov 29 #PHP
You might like
星际RPG字典
2020/03/04 星际争霸
PHP 身份验证方面的函数
2009/10/11 PHP
CodeIgniter上传图片成功的全部过程分享
2013/08/12 PHP
PHP记录搜索引擎蜘蛛访问网站足迹的方法
2015/04/15 PHP
基于Laravel实现的用户动态模块开发
2017/09/21 PHP
PHP实现求解最长公共子串问题的方法
2017/11/17 PHP
JS 拼图游戏 面向对象,注释完整。
2009/06/18 Javascript
Google 静态地图API实现代码
2010/11/19 Javascript
jQuery Tools Dateinput使用介绍
2012/07/14 Javascript
jquery(hide方法)隐藏指定元素实例
2013/11/11 Javascript
javascript去除空格方法小结
2015/05/21 Javascript
Node.js配合node-http-proxy解决本地开发ajax跨域问题
2016/08/31 Javascript
ES6新特性:使用export和import实现模块化详解
2017/07/31 Javascript
jQuery代码优化方法总结
2018/01/29 jQuery
Material(包括Material Icon)在Angular2中的使用详解
2018/02/11 Javascript
浅析vue中常见循环遍历指令的使用 v-for
2018/04/18 Javascript
微信小程序 bindtap 传参的实例代码
2020/02/21 Javascript
vue抽出组件并传值实例
2020/07/31 Javascript
[01:06:19]DOTA2-DPC中国联赛定级赛 LBZS vs SAG BO3第二场 1月8日
2021/03/11 DOTA
python笔记(1) 关于我们应不应该继续学习python
2012/10/24 Python
Python的Bottle框架的一些使用技巧介绍
2015/04/08 Python
python打开url并按指定块读取网页内容的方法
2015/04/29 Python
python 实现倒排索引的方法
2018/12/25 Python
Django模型序列化返回自然主键值示例代码
2019/06/12 Python
python多线程semaphore实现线程数控制的示例
2020/08/10 Python
python实现每天自动签到领积分的示例代码
2020/08/18 Python
为你的html5网页添加音效示例
2014/04/03 HTML / CSS
英国著名药妆店:Superdrug
2021/02/13 全球购物
初中班主任寄语
2014/04/04 职场文书
老公出轨后的保证书
2015/05/08 职场文书
员工手册编写范本
2015/05/14 职场文书
遗失证明范文
2015/06/19 职场文书
重阳节简报
2015/07/20 职场文书
2015年依法治校工作总结
2015/07/27 职场文书
Python 文本滚动播放器的实现代码
2021/04/25 Python
react合成事件与原生事件的相关理解
2021/05/13 Javascript