PHP实现验证码

博客: www.tanchengjin.com

 

验证码的好处

验证码在网站中起着重要的作用,验证码另一个重要作用是确保网站安全利益更大化,预防不法分子利用软件进行恶意非法注册,验证码的防护,能够在保证其他用户拥有良好体验的同时,也可以降低对网站或APP的维护成本,同时还可以有效的防止在网站或APP中灌水,登录发布不良信息。

 

PHP实现验证码时主要用到imagecreatetruecolor(),imagecolorallocate(),imagefill(),imagesetpixel(),imagechar(),imagearc()这些函数。面向方法编程~~

 

imagecreatetruecolor()用户创建验证码的画布(也就是图片资源)

 

imagecolorallocate() 用于填充颜色,如画布的背景颜色,字体的颜色,返回颜色资源

 

imagefill() 用于设置验证码画布的背景颜色

 

imagesetpixel() imagearc()用于设置干扰元素,分别生成像素与椭圆

 

imagechar()用于设置要生成的验证码文字

 

 

 

 

 

代码实现php验证码

 

使用本验证码类

安装

composer

composer require tanchengjin/captcha

 

github

git clone git@github.com:tanchengjin/captcha.git


 

直接使用

echo new Captcha($w,$h,$num)

链式方法

自定义要生成的字符串,如要生成纯数字的验证码

$captcha=new Captcha();
echo $captcha->setText('0123456789')->display();

校验验证码

$captcha->verify();

 

具体实现

 

class Captcha
{
    private $image=null;
        
        #验证法字符
    private $vCode='';
        
        #验证码长度
    private $vCodeNum=4;
        
        #可生成的验证码
    private $text='abcdefghjklmnopqrstuvwxyz0123456789';

    #长宽
    private $height=30;
    private $width=100;

    #通过析构方法传入画布高度,宽度,要生成的验证码个数
    public function __construct($w=100,$h=30,$num=4)
    {
        $this->width=$w;
        $this->height=$h;
        $this->vCodeNum=$num;
    }
        
        #封装具体实现
    private function call(){
            #创建验证码画布
        $this->createImage($this->width,$this->height);
        #向验证码画布写入验证码
        $this->writeText();
        #设置干扰元素
        $this->setDisturb();
        #图像输出
        $this->output();
    }

    private function startSession()
    {
        if(!isset($_SESSION)){
            session_start();
        }
    }
    #向session中写入验证码
    private function writeSession($key,$value)
    {
        $this->startSession();

        $_SESSION[$key]=strtoupper($value);
    }
    public function verify($code){
        $this->startSession();

        return strtoupper($code) == $_SESSION['code']?true:false;
    }
    
    private function createImage($w,$h)
    {
        $this->image=imagecreatetruecolor($w,$h);
        $color=imagecolorallocate($this->image,255,255,255);
        imagefill($this->image,0,0,$color);
    }

    private function createVerificationCode(){
        for ($i=0;$ivCodeNum;$i++){
            if(empty($this->vCode)){
                $this->vCode=$this->text{rand(0,strlen($this->text)-1)};
            }else{
                $this->vCode.=$this->text{rand(0,strlen($this->text)-1)};
            }
        }
        $this->writeSession('code',$this->vCode);
    }

    private function writeText()
    {
        $this->createVerificationCode();
        for ($i=0;$ivCode);$i++){
            $fontColor=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
            $fontSize=rand(5,8);
            $x=floor($this->width/$this->vCodeNum)*$i+5;
            $y=rand(0,$this->height-imagefontheight($fontSize));
            imagechar($this->image,$fontSize,$x,$y,$this->vCode{$i},$fontColor);
        }
    }

    private function setDisturb()
    {
        #set pixel
        for ($i=0;$iwidth*$this->height)/15;$i++){
            $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
            imagesetpixel($this->image,rand(0,$this->width),rand(0,$this->height),$color);
        }

        #sex arc
        for ($i=0;$iimage,rand(0,255),rand(0,255),rand(0,255));
            imagearc($this->image,rand(0,$this->width),rand(0,$this->height),rand(floor($this->width/2),$this->width),rand(floor($this->height/2),$this->height),20,360,$color);
        }


    }
    private function output()
    {
        if(imagetypes() & IMG_PNG){
            header('Content-type: image/png');
            imagepng($this->image);
        }elseif(imagetypes() & IMG_JPEG){
            header('Content-type: image/jpeg');
            imagepng($this->image);
        }elseif(imagetypes() & IMG_JPG){
            header('Content-type: image/jpg');
            imagepng($this->image);
        }elseif(imagetypes() & IMG_GIF){
            header('Content-type: image/gif');
            imagepng($this->image);
        }else{
            exit(404);
        }
    }
    public function __toString()
    {
        $this->call();
        return '';
    }

    public function __destruct()
    {
        if(isset($this->image)){
            imagedestroy($this->image);
        }

    }

    /**
      * 链式方法,设置可以生成的验证码字符
     * @param $text
     * @return $this
     */
    public function setText($text)
    {
        $this->text = $text;
        return $this;
    }
        #链式方法,输出图像
    public function display()
    {
        $this->call();
    }
}

 

公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值