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 相关文章推荐
PHP4引用文件语句的对比
Oct 09 PHP
PHP常用特殊运算符号和函数总结(php新手入门必看)
Feb 02 PHP
PHP判断变量是否为0的方法
Feb 08 PHP
ThinkPHP模板IF标签用法详解
Jul 01 PHP
Laravel 4.2 中队列服务(queue)使用感受
Oct 30 PHP
通过php添加xml文档内容的方法
Jan 23 PHP
PHP+Jquery与ajax相结合实现下拉淡出瀑布流效果【无需插件】
May 06 PHP
php简单实现短网址(短链)还原的方法(测试可用)
May 09 PHP
php 静态属性和静态方法区别详解
Apr 09 PHP
PHP调用API接口实现天气查询功能的示例
Sep 21 PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
Sep 13 PHP
Mac系统下搭建Nginx+php-fpm实例讲解
Dec 15 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 选项及相关信息函数库
2006/12/04 PHP
PHP编程中字符串处理的5个技巧小结
2007/11/13 PHP
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
2011/08/12 PHP
php实现的二叉树遍历算法示例
2017/06/15 PHP
yii2.0整合阿里云oss删除单个文件的方法
2017/09/19 PHP
PHP实现笛卡尔积算法的实例讲解
2019/12/22 PHP
JS实现div居中示例
2014/04/17 Javascript
使用ajax+jqtransform实现动态加载select
2014/12/01 Javascript
JavaScript中检查对象property的存在性方法介绍
2014/12/30 Javascript
为JS扩展Array.prototype.indexOf引发的问题及解决办法
2015/01/21 Javascript
JavaScript之WebSocket技术详解
2016/11/18 Javascript
网站申请不到支付宝接口、微信接口,免接口收款实现方式几种解决办法
2016/12/14 Javascript
JS仿QQ好友列表展开、收缩功能(第二篇)
2017/07/07 Javascript
jQuery Ajax 实现分页 kkpager插件实例代码
2017/08/10 jQuery
JavaScript canvas实现围绕旋转动画
2017/11/18 Javascript
React Router V4使用指南(精讲)
2018/09/17 Javascript
Nuxt.js开启SSR渲染的教程详解
2018/11/30 Javascript
微信小程序实现简易table表格
2020/06/19 Javascript
浅谈 Webpack 如何处理图片(开发、打包、优化)
2019/05/15 Javascript
ES6的循环与可迭代对象示例详解
2021/01/31 Javascript
tornado捕获和处理404错误的方法
2014/02/26 Python
python中as用法实例分析
2015/04/30 Python
Python正则表达式教程之三:贪婪/非贪婪特性
2017/03/02 Python
Python实现SSH远程登陆,并执行命令的方法(分享)
2017/05/08 Python
Python中用字符串调用函数或方法示例代码
2017/08/04 Python
Python Django给admin添加Action的方法实例详解
2019/04/29 Python
Python使用get_text()方法从大段html中提取文本的实例
2019/08/27 Python
win10安装tensorflow-gpu1.8.0详细完整步骤
2020/01/20 Python
《风筝》教学反思
2014/04/10 职场文书
应聘会计求职信
2014/06/11 职场文书
网站出售协议书范文
2014/10/10 职场文书
2015年企业新年寄语
2014/12/08 职场文书
学生个人评语大全
2015/01/04 职场文书
SQL语法CONSTRAINT约束操作详情
2022/01/18 MySQL
Redis如何实现验证码发送 以及限制每日发送次数
2022/04/18 Redis
SQL Server中搜索特定的对象
2022/05/25 SQL Server