侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

一个经典的PHP验证码类分享

php  /  管理员 发布于 7年前   196

我们通过PHP的GD库图像处理内容,设计一个验证码类Vcode。将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中。只要在创建对象时,为构造方法提供三个参数,包括创建验证码图片的宽度、高度及验证码字母个数,就可以成功创建一个验证码类的对象。该类的声明代码如下所示:

width = $width;   $this->height = $height;   $this->num = $num;   $this->code = $this->createcode(); //调用自己的方法  }   //获取字符的验证码, 用于保存在服务器中  function getcode() {   return $this->code;  }   //输出图像  function outimg() {   //创建背景 (颜色, 大小, 边框)   $this->createback();    //画字 (大小, 字体颜色)   $this->outstring();    //干扰元素(点, 线条)    $this->setdisturbcolor();   //输出图像   $this->printimg();  }   //创建背景  private function createback() {   //创建资源   $this->img = imagecreatetruecolor($this->width, $this->height);   //设置随机的背景颜色   $bgcolor = imagecolorallocate($this->img, rand(225, 255), rand(225, 255), rand(225, 255));    //设置背景填充   imagefill($this->img, 0, 0, $bgcolor);   //画边框   $bordercolor = imagecolorallocate($this->img, 0, 0, 0);     imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, $bordercolor);  }   //画字  private function outstring() {   for($i=0; $i<$this->num; $i++) {     $color= imagecolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128));      $fontsize=rand(3,5); //字体大小     $x = 3+($this->width/$this->num)*$i; //水平位置    $y = rand(0, imagefontheight($fontsize)-3);     //画出每个字符    imagechar($this->img, $fontsize, $x, $y, $this->code{$i}, $color);   }  }   //设置干扰元素  private function setdisturbcolor() {   //加上点数   for($i=0; $i<100; $i++) {    $color= imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));     imagesetpixel($this->img, rand(1, $this->width-2), rand(1, $this->height-2), $color);   }    //加线条   for($i=0; $i<10; $i++) {    $color= imagecolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));     imagearc($this->img,rand(-10, $this->width+10), rand(-10, $this->height+10), rand(30, 300), rand(30, 300), 55,44, $color);   }  }   //输出图像  private function printimg() {   if (imagetypes() & IMG_GIF) {     header("Content-type: image/gif");     imagegif($this->img);   } elseif (function_exists("imagejpeg")) {     header("Content-type: image/jpeg");     imagegif($this->img);   } elseif (imagetypes() & IMG_PNG) {     header("Content-type: image/png");     imagegif($this->img);   } else {     die("No image support in this PHP server");   }    }   //生成验证码字符串  private function createcode() {   $codes = "3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";    $code = "";    for($i=0; $i < $this->num; $i++) {    $code .=$codes{rand(0, strlen($codes)-1)};    }    return $code;  }   //用于自动销毁图像资源  function __destruct() {   imagedestroy($this->img);  }  }

在上面的脚本中,虽然声明验证码类Vcode的代码比较多,但细节都被封装在类中,只要直接输出对象,就可以向客户端浏览器中输出幅图片,并可以在浏览器表单中使用。另外本类自动获取验证码图片中的字符串,促成在服务的$_SESSION["code"]中。在提交表单时,只有用户在表单中输入验证码图片上显示的文字,并和服务器中保留的验证码字符串完全相同时,表单才可以提交成功。(注意:验证码在服务器端在$_SESSION["code"]中,所以必须开启session会话才能使用该类,)

  在下面的脚本code.php中,使用session_start()开启用户会话控制,然后包含验证码类Vcode所在文件vcode.class.php,创建该类对象并直接输出。就可以将随机生成的验证码图片发送出去,同时会自动将这个验证码字符串保存在服务器中一份。代码如下所示:

getcode(); //将验证码图片输出 $vcode->outimg();?>

 表单代码如下所示:

"; }else{  echo "输入不对!
"; }}?>
username:
password:
code:

PHP经典验证码类下载:

PHP验证码类.rar

您可能感兴趣的文章:

  • 一个漂亮的php验证码类(分享)
  • PHP生成图片验证码、点击切换实例
  • php发送短信验证码完成注册功能
  • PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
  • php5 图片验证码实现代码
  • ThinkPHP验证码使用简明教程
  • php中文字母数字验证码实现代码
  • PHP 验证码不显示只有一个小红叉的解决方法
  • PHP中文汉字验证码
  • PHP简单验证码功能机制实例详解


  • 上一条:
    php数组查找函数总结
    下一条:
    一个经典实用的PHP图像处理类分享
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客