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
Nov 25 PHP
?算你??的 PHP 程式大小
Dec 06 PHP
php动态生成JavaScript代码
Mar 09 PHP
用js进行url编码后用php反解以及用php实现js的escape功能函数总结
Feb 08 PHP
在WAMP环境下搭建ZendDebugger php调试工具的方法
Jul 18 PHP
深入解析PHP中的(伪)多线程与多进程
Jul 01 PHP
PHP执行Curl时报错提示CURL ERROR: Recv failure: Connection reset by peer的解决方法
Jun 26 PHP
PHP中addcslashes与stripcslashes函数用法分析
Jan 07 PHP
Thinkphp框架开发移动端接口(2)
Aug 18 PHP
详解Yii2.0 rules验证规则集合
Mar 21 PHP
PHP错误处理函数register_shutdown_function使用示例
Jul 03 PHP
PHP中通过getopt解析GNU C风格命令行选项
Nov 18 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
星际初学者游戏中永远要做的事
2020/03/04 星际争霸
php 表单数据的获取代码
2009/03/10 PHP
destoon各类调用汇总
2014/06/20 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
2016/03/21 PHP
PHP简单检测网址是否能够正常打开的方法
2016/09/04 PHP
PHP文件系统管理(实例讲解)
2017/09/19 PHP
PHP二维数组实现去除重复项的方法【保留各个键值】
2017/12/21 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
javascript globalStorage类代码
2009/06/04 Javascript
通过javascript设置css属性的代码
2009/12/28 Javascript
转换字符串为json对象的方法详解
2013/11/29 Javascript
Javascript 按位与赋值运算符 (&amp;=)使用介绍
2014/02/04 Javascript
jQuery实现级联菜单效果(仿淘宝首页菜单动画)
2014/04/10 Javascript
js取模(求余数)隔行变色
2014/05/15 Javascript
实例详解jQuery结合GridView控件的使用方法
2016/01/04 Javascript
深入理解MVC中的时间js格式化
2016/05/19 Javascript
JS中使用正则表达式g模式和非g模式的区别
2017/04/01 Javascript
Node.js开发第三方微信公众平台
2017/06/05 Javascript
JS自动生成动态HTML验证码页面
2017/06/14 Javascript
浅谈JavaScript find 方法不支持IE的问题
2017/09/28 Javascript
js实现数组和对象的深浅拷贝
2017/09/30 Javascript
vue移动端UI框架实现QQ侧边菜单组件
2018/03/09 Javascript
详解开发react应用最好用的脚手架 create-react-app
2018/04/24 Javascript
jQuery是用来干什么的 jquery其实就是一个js框架
2021/02/04 jQuery
python利用Tesseract识别验证码的方法示例
2019/01/21 Python
Python常驻任务实现接收外界参数代码解析
2020/07/21 Python
pycharm中使用request和Pytest进行接口测试的方法
2020/07/31 Python
Python request post上传文件常见要点
2020/11/20 Python
HTML5各种头部meta标签的功能(推荐)
2017/03/13 HTML / CSS
大一学生的职业生涯规划书范文
2014/01/19 职场文书
优秀通讯员事迹材料
2014/01/28 职场文书
社区领导班子四风问题原因分析及整改措施
2014/09/28 职场文书
2015年幼儿园中班工作总结
2015/04/25 职场文书
中国汉字听写大会观后感
2015/06/02 职场文书
《勇者辞职不干了》上卷BD发售宣传CM公开
2022/04/08 日漫