前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php实现算术验证码功能

php实现算术验证码功能

作者头像
砸漏
发布2020-10-21 15:56:34
1.7K0
发布2020-10-21 15:56:34
举报
文章被收录于专栏:恩蓝脚本

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

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

代码语言:javascript
复制
<?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的部分代码

代码语言:javascript
复制
<!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的测试

代码语言:javascript
复制
<?php
session_start();
$res = $_SESSION['res'];
$value = $_GET['code'];
if ($res == $value) {
  echo "test success";
}else{
  echo "test fail";
}

以上就是我的代码组成.

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档