Portfolio
Scripts and Websites I've made using PHP and MySQL mainly.
Snippets
Code snippets and mini tutorials
Contact
Contact me if you have any questions or queries
Home / Snippets / creating_captcha
This section is new and undergoing snippet addition, as of 13/6/11
Captchas are used a lot nowadays for things such as user registrations to try and make sure that it is a human filling out the form and not a bot/script.
This is how to make a simple one:
In this script where there is a link to "image.jpeg" I have used this image: http://blog.nielsendata.com/image.axd?picture=2008%2F10%2FcaptchaBackground.jpg So you may want to link directly to that, or find your own background image to use.
You could then link to this file like you would an image, e.g.
See example below (click image to refresh it):
Additional things you may want to think about include:
This section is new and undergoing snippet addition, as of 13/6/11
Creating a Captcha
Captchas are used a lot nowadays for things such as user registrations to try and make sure that it is a human filling out the form and not a bot/script.
This is how to make a simple one:
In this script where there is a link to "image.jpeg" I have used this image: http://blog.nielsendata.com/image.axd?picture=2008%2F10%2FcaptchaBackground.jpg So you may want to link directly to that, or find your own background image to use.
<?php
// Set the content-type
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/png'); // Force it to be displayed as an image
class Captcha
{
private $chars;
public $min_len;
public $max_len;
public $bg_img;
private $chars_text;
private $chars_num;
private $chars_both;
public function __construct($c)
{
$this->chars = $c; // Could be either: "a-z", "0-9", "both"
$this->min_len = 4; // Minimum length of captcha string
$this->max_len = 6; // Maximum length of captcha string
$this->bg_img = "image.jpeg";
$this->chars_text = "abcdefghijklmnopqrstuvwxyz";
$this->chars_num = "0123456789";
$this->chars_both = $this->chars_num . $this->chars_text . $this->chars_num;
}
public function draw()
{
// Create the image
$im = imagecreatefromjpeg($this->bg_img);
// Colour to write in (r,g,b)
$col = imagecolorallocate($im, 0, 0, 0);
// Create text to write
switch($this->chars)
{
case 'a-z':
$text = $this->chars_text;
break;
case '0-9':
$text = $this->chars_num;
break;
default:
$text = $this->chars_both;
break;
}
$text = $this->generateRandomString($text);
// Specify Font Size
$font = 5; // 1-5
// Specify x and y co-ords on image
$x = rand(1,50);
$y = rand(1,20);
// Add the text
imagestring($im, $font, $x, $y, $text, $col);
// Draw Image
imagepng($im);
imagedestroy($im);
}
public function generateRandomString($list)
{
$key = '';
$length = rand($this->min_len, $this->max_len);
for($i=0;$i<$length;$i++)
{
$key .= $list{rand(0,strlen($list)-1)};
}
return $key;
}
}
$myCaptcha = new Captcha("a-z");
$myCaptcha->draw();
?>
You could then link to this file like you would an image, e.g.
<img src="image.php" alt="Captcha" />
See example below (click image to refresh it):
Additional things you may want to think about include:
- Different fonts
- Distractions - Other smaller characters to confuse bots
- Better backgrounds
- Automatically generated backgrounds (e.g. drawing random lines and shapes on the background)
Comments
No Comments Have Been Made


