Simple Captcha in PHP

This captcha is simple and can use for any website ^^

<?php
require_once BASE_PATH . '/Code/claviska/simple-php-captcha.php';
class Captcha {
    /**
     * Document: https://github.com/claviska/simple-php-captcha
     */
    public static function checkCaptcha () {
        $isSuccess = FALSE;
        if (
            empty($_POST['simple_captcha_client']) === FALSE &&
            empty($_SESSION['simpleCaptcha']['code']) === FALSE &&
            strtolower($_POST['simple_captcha_client']) == strtolower($_SESSION['simpleCaptcha']['code'])
        ) {
            $isSuccess = TRUE;
        }
        return $isSuccess;
    }
    public static function generateCaptcha () {
        $_SESSION['simpleCaptcha'] = simple_php_captcha([
            'min_length' => 6,
            'max_length' => 6,
            'characters' => '123456789'
        ]);
        return $_SESSION['simpleCaptcha']['image_src'];
    }
    public static function showCaptcha ($class = NULL) {
        return '<div class="captcha-holder ' . $class . '"><img src="' . Captcha::generateCaptcha() . '" class="captcha-img" /><input type="text" name="simple_captcha_client" placeholder="' . convertCase(trans('validation.enter_captcha'), 3) . '" /><a data-href="' . BASE_URL . '/captcha/reset" class="captcha-refresh"><i class="icons fi-refresh"></i></a></div>';
    }
}
class Default_CaptchaController {
    function resetAction() {
        require_once BASE_PATH . '/Code/Captcha.php';
        echo Captcha::generateCaptcha();
    }
}
$('.captcha-refresh').on('click', function () {
	var $me = $(this);
	$.ajax({
		url: $(this).data('href')
	})
	.done(function(src) {
		$me.closest('.captcha-holder').find('.captcha-img').attr('src', src);
	});
});
.captcha-holder {
    &> * {
        display: inline-block;
        width: auto;
        height: 39px;
    }
    &> input {
        width: 125px;
    }
    .captcha-refresh {
        cursor: pointer;
        .icons {
            @include font-size(22px);
            margin-left: 5px;
            position: relative;
            top: 3px;
        }
    }
}


————-
Using:

<?php
if (Captcha::checkCaptcha() === TRUE) {
    ...
}
echo Captcha::showCaptcha();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s