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 相关文章推荐
在php中使用sockets:从新闻组中获取文章
Oct 09 PHP
使用 eAccelerator加速PHP代码的方法
Sep 30 PHP
PHP用身份证号获取星座和生肖的方法
Nov 07 PHP
PHP设计模式之观察者模式(Observer)详细介绍和代码实例
Apr 08 PHP
php去掉URL网址中带有PHPSESSID的配置方法
Jul 08 PHP
php使用sql server验证连接数据库的方法
Dec 25 PHP
分享PHP守护进程类
Dec 30 PHP
thinkphp3.2实现上传图片的控制器方法
Apr 28 PHP
给大家分享几个常用的PHP函数
Jan 15 PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
Sep 13 PHP
PHP实现按之字形顺序打印二叉树的方法
Jan 16 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
Mar 26 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脚本的10个技巧(7)
2006/10/09 PHP
php利用单例模式实现日志处理类库
2014/02/10 PHP
PHP命名空间(namespace)的动态访问及使用技巧
2014/08/18 PHP
PHP PDOStatement::rowCount讲解
2019/02/01 PHP
PHP实现简单日历类编写
2020/08/28 PHP
javascript实现二分查找法实现代码
2007/11/12 Javascript
xheditor与validate插件冲突的解决方案
2010/04/15 Javascript
javascript cookie操作类的实现代码小结附使用方法
2010/06/02 Javascript
jQuery 图片切换插件(代码比较少)
2012/05/07 Javascript
jQuery插件jRumble实现网页元素抖动
2015/06/05 Javascript
AngularJS应用开发思维之依赖注入3
2016/08/19 Javascript
Vue form 表单提交+ajax异步请求+分页效果
2017/04/22 Javascript
Vue中的数据监听和数据交互案例解析
2017/07/12 Javascript
如何理解Vue的render函数的具体用法
2017/08/30 Javascript
详解.vue文件解析的实现
2018/06/11 Javascript
关于js对textarea换行符的处理方法浅析
2018/08/03 Javascript
vue 基于element-ui 分页组件封装的实例代码
2018/12/10 Javascript
angular inputNumber指令输入框只能输入数字的实现
2019/12/03 Javascript
jQuery实现滑动开关效果
2020/08/02 jQuery
antd Select下拉菜单动态添加option里的内容操作
2020/11/02 Javascript
vue在图片上传的时候压缩图片
2020/11/18 Vue.js
JS实现点击掉落特效
2021/01/29 Javascript
[01:02:09]Liquid vs TNC 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21
2020/07/19 DOTA
python实现socket+threading处理多连接的方法
2019/07/23 Python
python制作朋友圈九宫格图片
2019/11/03 Python
用python实现学生管理系统
2020/07/24 Python
纯css3实现鼠标经过图片显示描述的动画效果
2014/09/01 HTML / CSS
优秀员工自荐信范文
2013/10/05 职场文书
工作室成员个人发展规划范文
2014/01/24 职场文书
城建学院毕业生自荐信
2014/01/31 职场文书
自我评价的范文
2014/02/02 职场文书
医院安全生产月活动总结
2014/07/05 职场文书
企业领导对照检查材料
2014/08/20 职场文书
2016春季田径运动会广播稿
2015/12/21 职场文书
4种非常实用的python内置数据结构
2021/04/28 Python
SQL Server中的游标介绍
2022/05/20 SQL Server