为了账号安全,请及时绑定邮箱和手机立即绑定

完整代码:PHP 登录验证码

标签:
PHP

<?php

/**

  • 这个是验证码制作经典类
    */
    class Vcode{
    private $width; //验证码宽度
    private $height; //验证码高度
    private $codeNum; //验证码字符个数
    private $disturbColorNum; //干扰元素数量
    private $checkCode; //验证码字符
    private $image; //验证码资源
    private $str; //生成的字符

    function __construct($width=80,$height=20,$codeNum=5,$nan=1){
    $this->width=$width;
    $this->height=$height;
    $this->codeNum=$codeNum;
    //设置验证码干扰程度;
    switch($nan){
    case 1:$this->disturbColorNum=rand(2, 5); break;
    case 2:$this->disturbColorNum=rand(5, 7); break;
    case 3:$this->disturbColorNum=rand(7, 9); break;
    case 4:$this->disturbColorNum=rand(11, 15); break;
    case 5:$this->disturbColorNum=rand(15, 20); break;
    default:$this->disturbColorNum=35;
    }
    $this->image=imagecreatetruecolor($this->width, $this->height);

    }

    /**

    • */
      function index(){
      $size=rand(3, 5);
      $bai=imagecolorallocate($this->image, 255, 255, 255);
      imagefill($this->image, 0, 0, $bai);
      $this->ganrao($this->disturbColorNum);
      $this->str($this->codeNum);

      for ($i=0;$i<count($this->str);$i++){
      $x=floor($this->width/$this->codeNum)*$i+3;
      $y=rand(0, $this->height-imagefontheight($size));
      imagechar($this->image, $size, $x, $y,$this->str[$i] , imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));
      }

      imagepng($this->image);
      $this->jiancha();
      }

    /**

    • 生成字符(大写字母 ,小写字母,数字)
    • @param int $num 生成字符的个数
      */
      function str($num){
      $str=array();
      for ($i=0;$i<$num;$i++){
      $arr=array(chr(rand(48, 57)),chr(rand(97,122)),chr(rand(65,90)));
      $str[]=$arr[rand(0, 2)];
      }
      $this->str=$str;
      }

    /**

    • 干扰点,线函数
      */
      function ganrao($num){
      //生成线
      for($i=0;$i<$num;$i++){
      imageline($this->image, rand(0,$this->width), rand(0, $this->height), rand(0,$this->width), rand(0, $this->height),imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));

      }
      //生成点
      for($i=0;$i<$num+rand(20,35);$i++){
      imagesetpixel($this->image, rand(0,$this->width), rand(0,$this->height), imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));
      }

    }

    /**

    • 检查 格式有更好的移植性
      */
      function jiancha(){

      if(function_exists("imagegif")){
      header("content-type:image/gif");
      }else if(function_exists("imageipeg")){
      header("content-type:image/jpeg");
      }else if(function_exists("imagepng")){
      header("content-type:image/png");
      }else if(function_exists("imagewbmp")){
      header("content-type:image/vnd.wap.wbmp");
      }else{
      die("在PHP服务器中不支持图像!");
      }

    }

    function session(){
    return $this->str;
    }
    function __destruct(){
    imagedestroy($this->image); //释放图像资源
    }

}

点击查看更多内容
10人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
PHP开发工程师
手记
粉丝
4
获赞与收藏
99

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消