Custom CAPTCHA

Posted by Admin on Oct 26 2008 | PHP Classes

A CAPTCHA is a program that protects websites against bots by generating and grading tests that humans can pass but current computer programs cannot. For example, humans can read distorted text as the one shown below, but current computer programs can’t:

Custom CAPTCHA Screenshot

The term CAPTCHA (for Completely Automated Turing Test To Tell Computers and Humans Apart) was coined in 2000 by Luis von Ahn, Manuel Blum, Nicholas Hopper and John Langford of Carnegie Mellon University. At the time, they developed the first CAPTCHA to be used by Yahoo.


CSSPagination is a pagination class which combines with Cascading Style Sheet for good looking style; CSSPagination has main function to split all records before they will be loaded into one website, to be several records in one page (you can determine how many records in one page). So, if you want to jump at the other page, you can choose once of them. CSSPagination is easy to use and good looking. I try to throw the complexity code. The most important is, that you can change the CSS code (style.css) to make it suitable with your own page style.

Download Cusutom CAPTCHA

The code of captcha.class.php:

Code:
<br />
<?php<br />
/**<br />
* @author Dodit Suprianto<br />
* Email: d0dit@yahoo.com<br />
* Website: http://doditsuprianto.com, http://goiklan.co.nr<br />
* Website: http://www.meozit.com, http://easyads.co.nr<br />
*<br />
* call captcha code <img src=captcha.class.php?length=&font=&size=&angel=&file=><br />
*<br />
* You can enter the parameter or not. Explaination:<br />
*<br />
* length : how much character will be shown. 4 character by default.<br />
* font : Type of font (ex. Arial.ttf). Georgia font by default<br />
* size : Size of font character. 20 by default<br />
* angel : angel of font character. 0 by default, it means horizontal position<br />
* file : if you want subtitute the background image. "eyes.gif" by default.<br />
*<br />
* You must copy several file to current directory<br />
* if you want to subtitute the default setting.<br />
* such as font file, background file<br />
*<br />
*/</p>
<p>session_start();</p>
<p>class captcha<br />
{<br />
private $length;<br />
private $font;<br />
private $size;<br />
private $angel;<br />
private $file;</p>
<p>public function __construct()<br />
{<br />
$this->length = 4;<br />
$this->font = "georgia.ttf";<br />
$this->size = 20;<br />
$this->angel = 0;<br />
$this->file = "eyes.gif";<br />
}</p>
<p>public function setFile($file)<br />
{<br />
$this->file = $file;<br />
}</p>
<p>public function setLength($length)<br />
{<br />
$this->length = $length;<br />
}</p>
<p>public function setFont($font)<br />
{<br />
$this->font = $font;<br />
}</p>
<p>public function setSize($size)<br />
{<br />
$this->size = $size;<br />
}</p>
<p>public function setAngel($angel)<br />
{<br />
$this->angel = $angel;<br />
}</p>
<p>private function RandomText()<br />
{<br />
$output = "";<br />
$input = array('A','B','C','D','E','F','G','H','I','J','K','L',<br />
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2',<br />
'3','4','5','6','7','8','9');<br />
srand((float) microtime() * 10000000);<br />
$rand_keys = array_rand($input, count($input)); </p>
<p>for ($i=0; $i < count($input)-1; $i++)<br />
{<br />
$output .= $input[$rand_keys[$i]];<br />
}<br />
return substr($output, 0, $this->length);<br />
}</p>
<p>public function ConvertFontToImage()<br />
{<br />
$type = getimagesize($this->file);<br />
$random = $this->RandomText();<br />
switch ($type[2])<br />
{<br />
case IMAGETYPE_GIF:<br />
header("Content-type: image/gif");<br />
$im = imagecreatefromgif($this->file);<br />
$white = imagecolorallocate($im, 255, 255, 255);<br />
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());<br />
$px = ($type[0] - $textbox[4])/2;<br />
$py = ($type[1] - $textbox[5])/2;<br />
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);<br />
imagegif($im);<br />
imagedestroy($im);<br />
break;<br />
case IMAGETYPE_JPEG:<br />
header("Content-type: image/jpeg");<br />
$im = imagecreatefromjpeg($this->file);<br />
$white = imagecolorallocate($im, 255, 255, 255);<br />
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());<br />
$px = ($type[0] - $textbox[4])/2;<br />
$py = ($type[1] - $textbox[5])/2;<br />
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);<br />
imagejpeg($im);<br />
imagedestroy($im);<br />
break;<br />
case IMAGETYPE_PNG:<br />
header("Content-type: image/png");<br />
$im = imagecreatefrompng($this->file);<br />
$white = imagecolorallocate($im, 255, 255, 255);<br />
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());<br />
$px = ($type[0] - $textbox[4])/2;<br />
$py = ($type[1] - $textbox[5])/2;<br />
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);<br />
imagepng($im);<br />
imagedestroy($im);<br />
break;<br />
}<br />
$_SESSION['code'] = $random;<br />
}<br />
} </p>
<p>$handle = new captcha();<br />
if (!empty($_GET['length'])) $handle->setLength($_GET['length']);<br />
if (!empty($_GET['font'])) $handle->setFont($_GET['font']);<br />
if (!empty($_GET['size'])) $handle->setSize($_GET['size']);<br />
if (!empty($_GET['angel'])) $handle->setAngel($_GET['angel']);<br />
if (!empty($_GET['file'])) $handle->setFile($_GET['file']);<br />
$handle->ConvertFontToImage();<br />
?><br />

Testing file code:

Code:
<br />
<?php<br />
/**<br />
* @author Dodit Suprianto<br />
* Email: d0dit@yahoo.com<br />
* Website: http://doditsuprianto.com, http://goiklan.co.nr<br />
* Website: http://www.meozit.com, http://easyads.co.nr<br />
*<br />
* call captcha code <img src=captcha.class.php?length=&font=&size=&angel=&file=><br />
*<br />
* You can enter the parameter or not. Explaination:<br />
*<br />
* length : how much character will be shown. 4 character by default.<br />
* font : Type of font (ex. Arial.ttf). Georgia font by default<br />
* size : Size of font character. 20 by default<br />
* angel : angel of font character. 0 by default, it means horizontal position<br />
* file : if you want subtitute the background image. "eyes.gif" by default.<br />
*<br />
* You must copy several file to current directory<br />
* if you want to subtitute the default setting.<br />
* such as font file, background file<br />
*<br />
*/<br />
session_start();<br />
?><br />
<html><br />
<head></p>
<p></head><br />
<body><br />
<?php<br />
if ($_POST['submit'])<br />
{<br />
if ($_SESSION['code'] == $_POST['code'] && !empty($_SESSION['code']))<br />
{<br />
/**<br />
* you should add other codes in here<br />
* in this section, means that the captcha and code are match<br />
*/</p>
<p>echo "captcha is correct";<br />
unset($_SESSION['code']);<br />
}<br />
else<br />
{<br />
echo "captcha is wrong";<br />
}<br />
}<br />
echo "<br />
<form action='$_SERVER[SELF]' method=POST>";<br />
echo "Security code: <img src=captcha.class.php?length=4&font=&size=24&angel=5&file=>
<p>";<br />
echo "Input code:<br />
<input type=text name=code>";<br />
echo "<br />
<input type='submit' name='submit' value='Submit'>";<br />
echo "</form>
<p>";<br />
?><br />
</body><br />
</html><br />

Tags: , ,

2 comments for now

2 Responses to “Custom CAPTCHA”

  1. Thanks! Nice post.

    10 Nov 2008 at 10:27 pm

  2. thanks

    10 Nov 2008 at 11:47 pm

Trackback URI | Comments RSS

Leave a Reply