php验证码生成器


Posted in PHP onMay 24, 2017

现在很多网站都有实现用户集。然而为了防止机器人的网络攻击。限制登陆或者注册是有必要的。
在注册和登陆时强制要求输入一个机器难以识别的字符串集是一个不错的选择。虽然不能解决根本问题,但至少可以增加他们的成本。

利用PHP生成验证码需要用到GD2库。GD2库引用方法网络上有很多,不同操作系统导入方式也不同。

这段代码运行在WINDOS服务器平台

<?php
$iC = new idCode(5,60,30);
$iC->createPNG();

class idCode{
  private $words = array('a','b',
  'c','d','e','f','g','h','i','j','k','l',
  'm','n','o','p','q','r','s','t','u','v',
  'w','x','y','z','A','B','C','D','E','F',
  'G','H','I','J','K','L','M','N','O','P',
  'Q','R','S','T','U','V','W','X','Y','Z',
  '0','1','2','3','4','5','6','7','8','9');
  private $fonts;
  private $count;//验证码字符数
  private $height;
  private $width;
  private $path = '..\myfolder\fonts';
  private $keys;

  //构造函数
  public function __construct($count,$width,$height){
    $this->count = $count;
    $this->getFonts();
    $this->height = $height;
    $this->width = $width;
  }

  private function getFonts(){
    $dir = dir($this->path);

    while(false !== ($file = $dir->read())){
        if($file != '.' && $file != '..'){
          $this->fonts[count($this->fonts)] = basename($file);
        }
    }
    $dir->close();
  }

  private function createKeys(){
    for($i = 0;$i < $this->count;$i++){
      $this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)];
      //使用字体路径标识
      $this->keys[$i]['filename'] = $this->path.'\\'.$this->fonts[rand(0,count($this->fonts)-1)];
    }
  }

  public function createPNG(){
    $this->createKeys();

    //创建画布以及颜色块儿
    $bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//两边留10px空白,上下3px
    $grey = imagecolorallocate($bg,155,155,155);
    $blue = imagecolorallocate($bg,0x00,0x00,0xff);
    //填充背景
    imagefill($bg,0,0,$grey);
    //添加字符
    $pwidth = $this->width/$this->count;
    $x;$y;
    for($i = 0;$i < $this->count;$i++){
      $rotation = rand(-40,40);//偏转角度±40°
      $fontsize = 33;
      $width_txt;
      $height_txt;

      do{
        $fontsize--;
        $bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']);
        $width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上
        $height_txt = $bbox[7] - $bbox[1];
      }while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth));

      $fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255));
      $x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x坐标基本位置
      $y = $this->height/2 - $height_txt/2;

      imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']);
    }
    //绘制干扰线
    //根据字体酌情增加干扰线
    imageline($bg,0,15,40,10,$blue);
    //图像输出头文件
    header('Content-type:image/png');
    //输出png图像
    imagepng($bg);
    //清除缓存资源
    imagedestroy($bg);
  }

  public function checkKeys($input){
    if(count($input)!=$this->count){
      return 'ERROR:长度不正确.';
    }else{
      for($i=0;$i < $this->count;$i++){
        //0 o O I l 1 校准,根据所选择的字体确定是否需要手动校准
        if($input[$i] != $this->keys[$i]['char']){
          return 'SUCCESS.';
        }else{
          return 'ERROR:请输入正确验证码.';
        }
      }
    }
  }
}
?>

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

PHP 相关文章推荐
ADODB结合SMARTY使用~超级强
Nov 25 PHP
PHP产生随机字符串函数
Dec 06 PHP
Memcache 在PHP中的使用技巧
Feb 08 PHP
删除无限分类并同时删除它下面的所有子分类的方法
Aug 08 PHP
PHP写的资源下载防盗链类分享
May 12 PHP
PHP函数extension_loaded()用法实例
Jan 19 PHP
php实现SAE上使用storage上传与下载文件的方法
Jun 29 PHP
PHP+JS实现大规模数据提交的方法
Jul 02 PHP
PHP如何通过AJAX方式实现登录功能
Nov 23 PHP
PHP实现长文章分页实例代码(附源码)
Feb 03 PHP
Yii2第三方类库插件Imagine的安装和使用
Jul 06 PHP
php使用array_chunk函数将一个数组分割成多个数组
Dec 05 PHP
php批量修改表结构实例
May 24 #PHP
php 人员权限管理(RBAC)实例(推荐)
May 24 #PHP
老生常谈PHP面向对象之命令模式(必看篇)
May 24 #PHP
php实现查询功能(数据访问)
May 23 #PHP
php批量删除操作(数据访问)
May 23 #PHP
[原创]PHP正则删除html代码中a标签并保留标签内容的方法
May 23 #PHP
php出租房数据管理及搜索页面
May 23 #PHP
You might like
PHP字符串比较函数strcmp()和strcasecmp()使用总结
2014/11/19 PHP
php使用递归计算文件夹大小
2014/12/24 PHP
Yii框架参数化查询中IN查询只能查询一个的解决方法
2017/05/20 PHP
PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例
2017/08/10 PHP
Laravel5.5 视图 - 创建视图和数据传递示例
2019/10/21 PHP
PHP如何使用cURL实现Get和Post请求
2020/07/11 PHP
JavaScript 中的replace方法说明
2007/04/13 Javascript
使用GruntJS构建Web程序之合并压缩篇
2014/06/06 Javascript
jQuery实现伸展与合拢panel的方法
2015/04/30 Javascript
原生javascript实现的一个简单动画效果
2016/03/30 Javascript
Javascript自执行匿名函数(function() { })()的原理浅析
2016/05/15 Javascript
关于数据与后端进行交流匹配(点亮星星)
2016/08/03 Javascript
jquery easyui DataGrid简单示例
2017/01/23 Javascript
bootstrap动态添加面包屑(breadcrumb)及其响应事件的方法
2017/05/25 Javascript
微信小程序带动画弹窗组件使用方法详解
2018/11/27 Javascript
微信二次分享报错invalid signature问题及解决方法
2019/04/01 Javascript
详解jQuery中的getAll()和cleanData()
2019/04/15 jQuery
Vue Elenent实现表格相同数据列合并
2020/11/30 Vue.js
[01:01]青春无憾,一战成名——DOTA2全国高校联赛开启
2018/02/25 DOTA
python getopt 参数处理小示例
2009/06/09 Python
python让图片按照exif信息里的创建时间进行排序的方法
2015/03/16 Python
Python获取SQLite查询结果表列名的方法
2017/06/21 Python
Python元组及文件核心对象类型详解
2018/02/11 Python
使用Numpy读取CSV文件,并进行行列删除的操作方法
2018/07/04 Python
Python实现使用request模块下载图片demo示例
2019/05/24 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
2019/06/27 Python
pytorch实现线性拟合方式
2020/01/15 Python
html5 视频播放解决方案
2016/11/06 HTML / CSS
servlet面试题
2012/08/20 面试题
优秀士兵个人事迹材料
2014/01/19 职场文书
公司门卫岗位职责范本
2014/07/08 职场文书
学习十八大宣传标语
2014/10/09 职场文书
2015年党员个人剖析材料
2014/12/18 职场文书
解除处分决定书
2015/06/25 职场文书
商务信函英语问候语
2015/11/10 职场文书
postman中form-data、x-www-form-urlencoded、raw、binary的区别介绍
2022/01/18 HTML / CSS