PHP code example of bugcat / captcha

1. Go to this page and download the library: Download bugcat/captcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bugcat / captcha example snippets


    use Bugcat\Captcha\Captcha;

    $captcha = new Captcha();

    $config = []; //custom configuration
    $codes = $captcha->gnrt($config);
    $_SESSION['captcha'] = $codes;

    $captcha->img();

    if ( $_POST['captcha'] == $_SESSION['captcha'] ) {
        //do something.
    } else {
        //do something.
    }

    $captcha = new Captcha();
    var_dump($captcha::CFG_EXP);

    $config = [
        //Whether need a border, 1 is need and 0 is not, default 1.
        'border'   => '1|0', 
        
        //The verification code length, 1 at least, default 4.
        'charnum'  => '4', 
        
        //The fontsize of captcha,  using px, 8 at least, default 16.
        'fontsize' => '16',
        
        //The code direction, 0 is horizontal and  1 is vertical, default 0.
        'codedir'  => '1|0',
        
        //The font of code, use existing list or custom, default simhei.
        //This package provides these fonts: simhei, ABeeZee_regular, Asap_700, Khand_500, Open_Sans_regular, Roboto_regular, Ubuntu_regular, ygyxsziti2.0 .
        //If use custom, it needs the full and usable path.
        'font'     => 'simhei|Roboto_regular|/var/www/html/test/fonts/custom.ttf',
        
        //The character pool for captcha, use existing list or custom, default CHS.
        //This package provides these characters: 
        //  CHS : Chinese Simplified
        //  CHT : Chinese Traditional
        //  ENG : 2346789ABCDEFGHJMNPQRTUXYZ
        'text'     => 'CHS|CHT|ENG|custom string allow spaces because it will be ignored',
    ];

use Bugcat\Captcha\Captcha;

class Auth
{
    private $captcha;

    public function __construct()
    {
        $this->captcha = new Captcha();
    }

    public function vcode()
    {
        $config = ['charnum' => 2, 'text' => 'CHT'];
        $codes = $this->captcha->gnrt($config);
        $_SESSION['captcha'] = $codes;
        
        $this->captcha->img();
    }
    
    public function register()
    {
        if ( $_POST['CSRF'] ) {
            if ( !$this->verify() ) {
                die( 'the verification code is error.' );
            }
            //do something.
        }
        //do something.
    }
    
    private function verify()
    {
        if ( empty($_POST['captcha']) || empty($_SESSION['captcha']) ) {
            return false;
        }
        if ( $_POST['captcha'] == $_SESSION['captcha'] ) {
            return true;
        } else {
            retrun false;
        }
    }
    
}