如何在PHP中利用session与gd库实现一个验证码生成功能

发布时间:2020-12-23 15:25:14 作者:Leah
来源:亿速云 阅读:144

这篇文章将为大家详细讲解有关如何在PHP中利用session与gd库实现一个验证码生成功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的。目前大大小小的网站都有验证码。今天自己实现了一个简单的验证码类。说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下。当然,因为字体的原因,要想一眼看出来并不容易。同时,为了避免字母的大小写与数字混淆,又去掉了那些看起来很像的字母数字。
类:

<?php
/**
 *简单生成验证码类
 */
class Captcha {
  private $width;//验证码宽度
  private $height;//验证码高度
  private $countOfChars;//字符数
  //private $distrubLines;//干扰线条数
  private $chars;//随机生成的字符串

  public function __construct($width=100,$height=30,$countOfChars=4,$distrubLines=2) {
  //初始化参数
  $this->width=$width;
  $this->height=$height;
  $this->countOfChars=$countOfChars;
  session_start();
  }

  /**
   * 执行全部动作,生成验证码并直接输出
   */
  public function execute(){
  $imageHandle=$this->createImage();
  $this->createChars();
  $this->drawChars($imageHandle);
  $this->outImage($imageHandle);
  }

  /**
   * 创建画布,并随机填充颜色
   * @return 返回画布句柄
   */
  public function createImage(){
  $imageHandle= imagecreate($this->width, $this->height);
  //随机背景颜色
    $randColor=imagecolorallocate($imageHandle, 50, mt_rand(0, 50), mt_rand(0, 50));
  imagefill($imageHandle, 0, 0, $randColor);
  return $imageHandle;
  }

  /**
   * 生成随机字符
   */
  private function createChars(){
  //候选字符
  $str='ABCDEFGHJKLMNPQRSTUVWXZabcdefghijkmnpqtuvwx2346789';
  $chars='';
  for($i=0;$i<$this->countOfChars;$i++){
    $chars.=$str[mt_rand(0,strlen($str)-1)];
  }
  $this->chars=$chars;
  //保存在会话中
  $_SESSION['captcha']=strtolower($chars);
  }

  /**
   * 将字符写入图像
   * @param type $imageHandle 图像句柄
   */
  private function drawChars($imageHandle){
  if($this->chars!=null){
    $font='/home/WWW/YuWeiLiShuFT.ttf';
    for($i=0;$i<strlen($this->chars);$i++){
    $color= imagecolorallocate($imageHandle,mt_rand(50, 200),mt_rand(100, 255),255);
    imagefttext($imageHandle,30, 30,$i*20+10,25,$color,$font,$this->chars[$i]);
    }
  }
  }

  /**
   * 输出图像
   * @param type $imageHandle 图像句柄
   */
  private function outImage($imageHandle){
  imagepng($imageHandle);
  imagedestroy($imageHandle);
  }

  /**
   * 判断用户输入的验证码是否正确
   * @param type $usrInput 用户的输入
   * @return boolean 验证码是否匹配
   */
  public static function isRight($usrInput){
  if(isset($_SESSION['captcha'])){
    if(strtolower($usrInput)==$_SESSION['captcha']){
    $_SESSION['captcha']=null;
    return true;
    }else{
    $_SESSION['captcha']=null;
    return false;
    }
  }
  }
}

把验证设置成了静态方法,因为生成验证码后已经把验证码存到了session中,验证时直接调用静态方法,而不需要实例化这个类了。

上面的字体可以随意设置。

下面的代码讲返回一个图像,实例化Captcha类后动态生成的一个图像。(outCaptcha.php)

<?php

require('Captcha.php');
$code= new Captcha();
header('Content-Type:image/png');
$code->execute();

header(‘Content-Type:image/png');

这句话的作用是告诉浏览器输出的是png图像,而不是html代码。浏览器收到后就将下面的输出解析成图像。

然后写一个html静态页面(testCaptcha.html),创建表单

<!DOCTYPE html>
<html>
  <head>
    <title>验证码测试</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <h2>请输入验证码:</h2>
    <img src="outCaptcha.php"/>
    <form method="POST" action="prove.php">
      <input type="text" name="input_captcha"/>
      <button name="submit">确定</button>
    </form>
  </body>
</html>

仅仅是这样是不够的,看到表单提交的地址了么?那个就是用来验证验证码是否输入正确的代码:

session_start();
$inputCaptcha= trim($_POST['input_captcha']);
require('Captcha.php');
if(Captcha::isRight($inputCaptcha)){
  echo '验证码正确';
}else{
  echo '验证码错误或已过期';
}
session_destroy();

关于如何在PHP中利用session与gd库实现一个验证码生成功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 怎么在PHP中使用GD库实现一个验证码功能
  2. 如何在PHP中使用GD2函数库实现一个验证码功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php session gd库

上一篇:利用destoon怎么在底部添加访问者

下一篇:利用php项目对多行文本进行格式化

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》