EM Captcha – Visual PHP Captcha Class

I’ve recently been developing a visual PHP Captcha Class for use in a CMS (content management system) project I’m currently working on as an alternative to Google’s “reCAPTCHA”, for people who don’t know how, or don’t want to set up “reCAPTCHA”.

At the moment it’s a relatively simple captcha class that’s easy to implement, I’ve already started thinking about improvements that I can make in the future.

Here is an example as to how to use the captcha class:

New example file (version 1.2)

<?php
date_default_timezone_set("Europe/London");
/* EM Captcha Implementation Example */
require(__DIR__ . "/captcha.class.php"); // Include the captcha class file "captcha.class.php"
 
if (isset($_POST['submitButton'])) { // If the form has been submitted, process it
	// Create a new captcha object parsing in the hidden ID in the form, this will always be from the "emCaptchaID" POST variable
	$captcha = new Captcha(filter_input(INPUT_POST, 'emCaptchaID', FILTER_SANITIZE_NUMBER_INT)); 
 
    if ($captcha->isValid()) { // If the input captcha matches the image
		print("Valid captcha!");
		// Your code here..
 
	} else { // If the input captcha does not match the image
		print("Invalid captcha!");
		// Your code here..
	}
 
} else { // If the form has not been submitted, display it
	$captcha = new Captcha(); // Create a new captcha object, as we're not validating captcha input we don't parse in an ID
 
	// Set options, White background, show border, 5 minutes until the captcha expires, "Answer:" will be shown above the text field
	$captcha->setOpts("white", true, 5, "Answer:"); 
 
	// Output a HTML form
	print("<form method=\"post\">");
 
	// Call $captcha->output() where you want the captcha image and text field to appear
	$captcha->output("./"); // "./" is the path to the captcha_show.php file from the current location, default is __DIR__
	print("<br /><button type=\"submit\" name=\"submitButton\">Submit</button>");
	print("</form>");
}

 

Old example file (version 1.0 and 1.1)

<?php
/* EM Captcha Implementation Example */
require(__DIR__ . "/captcha.class.php"); // Include the captcha class file "captcha.class.php"
 
if (isset($_POST['submitButton'])) { // If the form has been submitted, process it
	// Create a new captcha object parsing in the hidden ID in the form, this will always be "$_POST['emCaptchaID']"
	$captcha = new Captcha($_POST['emCaptchaID']); 
 
    if ($captcha->isValid()) { // If the input captcha matches the image
		print("Valid captcha!");
		// Your code here..
 
	} else { // If the input captcha does not match the image
		print("Invalid captcha!");
		// Your code here..
	}
 
} else { // If the form has not been submitted, display it
	$captcha = new Captcha(); // Create a new captcha object, as we're not validating captcha input we don't parse in an ID
 
	// Set options, White background, show border, 5 minutes until the captcha expires, "Answer:" will be shown above the text field
	$captcha->setOpts("white", true, 5, "Answer:"); 
 
	// Output a HTML form
	print("<form method=\"post\">");
 
	// Call $captcha->output() where you want the captcha image and text field to appear
	$captcha->output("./"); // "./" is the path to the captcha_show.php file from the current location, default is __DIR__
	print("<br /><button type=\"submit\" name=\"submitButton\">Submit</button>");
	print("</form>");
}
?>

If the form had not been submitted the above code will output a form which would look like the screen shot below:

EM Captcha 1.0 Screen Shot
EM Captcha 1.0 – BETA

 

Extra Information

PHP extension / library requirements: SQLite3 and GD (Version 2.0)

License:  Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)

True Type Fonts: 3Dumb, LimeLight, SpecialElite – Note, these fonts are subject to their own licenses which are included in the download under the “fonts” directory.

 

Download

1 thought on “EM Captcha – Visual PHP Captcha Class

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *