温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么实现随机数字、字母的验证码功能

发布时间:2021-05-22 16:30:18 来源:亿速云 阅读:263 作者:Leah 栏目:开发技术

使用PHP怎么实现随机数字、字母的验证码功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1、classgd.class.php

<?php
Class Captcha{
    private $_fontfile='';
    private $_size=36;
    private $_width=200;
    private $_height=100;
    private $_length=4;
    private $_image=null;
    private $_snow=0;
    private $_pixel=0;
    private $_line=0;
  public function __construct($config=array()){
    if(is_array($config)&&count($config)>0){
      if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
        $this->_fontfile=$config['fontfile'];
      }else{
        return false;
      }
      if(isset($config['size'])&&$config['size']>0){
        $this->_size=(int)$config['size'];
      }
      if(isset($config['width'])&&$config['width']>0){
        $this->_width=(int)$config['width'];
      }
      if(isset($config['height'])&&$config['height']>0){
        $this->_height=(int)$config['height'];
      }
      if(isset($config['length'])&&$config['length']>0){
        $this->_length=(int)$config['length'];
      }
      if(isset($config['snow'])&&$config['snow']>0){
        $this->_snow=(int)$config['snow'];
      }
      if(isset($config['pixel'])&&$config['pixel']>0){
        $this->_pixel=(int)$config['pixel'];
      }
      if(isset($config['line'])&&$config['line']>0){
        $this->_line=(int)$config['line'];
      }
      $this->_image=imagecreatetruecolor($this->_width,$this->_height);
      return $this->_image;
     }
     else{
      return false;
    }
  }
  public function getCaptcha(){
    $white=imagecolorallocate($this->_image,255,255,255);
    imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
    $str=$this->_generateStr($this->_length);
    if(false===$str){
      return false;
    }
    $fontfile=$this->_fontfile;
    for($i=0;$i<$this->_length;$i++){
      $size=$this->_size;
      $angle=mt_rand(-30,30);
      $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
      $y=ceil($this->_height/1.5);
      $color=$this->_getRandColor();
      //针对中文字符截取
      //$text=mb_substr($str,$i,1,'utf-8');
      $text=$str{$i};
      imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);
    }
    if($this->_snow){
      $this->_getSnow();
    }else{
      if($this->_pixel){
        $this->_getPixel();
      }
      if($this->_line){
        $this->_getLine();
      }
    }
    header('content-type:image/png');
    imagepng($this->_image);
    imagedestroy($this->_image);
    return strtolower($str);
  }
  private function _getSnow(){
    for($i=1;$i<=$this->_snow;$i++){
      imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor());
    }
  }
  private function _getPixel(){
    for($i=1;$i<=$this->_pixel;$i++){
      imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
    }
  }
  private function _getLine(){
    for($i=1;$i<=$this->_line;$i++){
      imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
    }
  }
  private function _generateStr($length=4){
    if($length<1 || $length>30){
      return false;
    }
    $chars=array(
      'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z',
      'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z',
      1,2,3,4,5,6,7,8,9
      );
    $str=join('',array_rand(array_flip($chars),$length));
    return $str;
  }
  private function _getRandColor(){
    return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  }
}
?>

 2、testCaptcha.php

<?php
require_once 'classgd.class.php';
$config=array(
'fontfile'=>'fonts/simfang.ttf',  //引入字体文件
//'snow'=>50,
'pixel'=>100,
'line'=>10
  );
$captcha=new Captcha($config);
$captcha->getCaptcha();
?>

PHP开发环境搭建工具有哪些

一、phpStudy,是一个新手入门最常用的开发环境。二、WampServer,WampServer也同样的也是和phpStudy一样操作简单对小白比较友好。三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包;四、MAMP,MAMP分为两种MAMP和MAMP Pro for Mac。五、宝塔面板,宝塔面板是一款服务器管理软件,支持windows和linux系统。六、UPUPW,UPUPW是目前Windows平台下最具特色的Web服务器PHP套件。

看完上述内容,你们掌握使用PHP怎么实现随机数字、字母的验证码功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI