扣丁学堂PHP培训简述PHP如何实现算术验证码功能

2019-04-15 10:54:28 2226浏览

本篇文章扣丁学堂PHP培训小编给读者们分享一下PHP如何实现算术验证码功能,文中有代码列出供大家参考学习,希望和小伙伴们共同参考。



扣丁学堂PHP培训简述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培训官网、微信等平台,扣丁学堂IT职业在线学习教育有专业的PHP讲师为您指导,此外扣丁学堂老师精心推出的PHP视频教程定能让你快速掌握PHP从入门到精通开发实战技能。扣丁学堂PHP技术交流群:374332265。


扣丁学堂微信公众号                          Python全栈开发爬虫人工智能机器学习数据分析免费公开课直播间


【关注微信公众号获取更多学习资料】         【扫码进入Python全栈开发免费公开课】




查看更多关于“php培训资讯”的相关文章>>



标签: PHP培训 PHP基础教程 PHP学习视频 PHP教学视频 PHP入门教程 PHP教程视频 PHP在线学习 PHP在线视频 PHP在线教程 扣丁学堂PHP培训

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号