PHP CAPTCHA Generator
CAPTCHA stands for Completely Automated Public Turing test to tell Computers and Humans Apart. CAPTCHA is used in public web form(such as: sign up form, log in form, contact us form, and so on) to determine whether a user is a human or computer(bot). A CAPTCHA simply provide an image with a random letters printed in it and let the user to type those letters in a provided text box. In this article, we will create a CAPTCHA using PHP.
Requirement
To create a CAPTCHA using PHP, the GD PHP extension is required to handle graphical functionalities. The GD extension will allow us to create an image on the fly.
Source Code
First, we need to create the CAPTCHA generator PHP script. Let’s name it with captcha.php.
[php]<?
//By : Isusx Programming Corner
//URL : http://isusx.com
//Starting a session. We need to save the generated random letters
//for verification use
session_start();
//Charactes used in verification
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
//Uncomment the following line if your PHP version in lower than 4.20
//srand((float) microtime() * 10000000);
//Generate a verification key that consists of 5 random letters
$key = substr(str_shuffle($chars), 0, 5);
//Storing verification key in session (the key should be
//encrypted to make it more secure
$_SESSION['verify_key'] = md5($key);
//Build a CAPTCHA background collection to make it harder
//to be read by bot
//List your background image file name for your CAPTCHA
//You need to choose several images and put in a folder named <em>images
</em>//The background image should be small images (such as 200px x 75px) <em>
</em>//The <em>images </em>folder should be put in the same directory as this script
$bg_files = array(
"blue.jpg",
"simple.jpg",
"factory.jpg",
"boxes.jpg"
);
//Pick a random background image
$selected_bg = $bg_files[array_rand($bg_files)];
//Generating CAPTCHA base image
//Set your captcha image size (in pixel)
$img_w = 75; //Image width
$img_h = 35; //Image height
$img = imagecreatetruecolor($img_w, $img_h);
$img_bg = imagecreatefromjpeg("images/".$selected_bg);
imagecopyresized(
$img,
$img_bg,
0, 0, 0, 0,
imagesx($img),
imagesy($img),
imagesx($img_bg),
imagesy($img_bg)
);
//Printing generated verification key on the image
//Set font type
$font_type = 5;
//Set font color using RGB code (0,0,0 represents BLACK)
$font_color = imagecolorallocate($img, 0,0,0);
$x = 15;
$y = (imagesy($img)-imagefontheight($font_type))/2;
imagestring($img, $font_type, $x, $y, $key, $font_color);
//Generate image output
header(’Content-type:image/jpeg’);
imagejpeg($img);
imagedestroy($img);
?>[/php]
After you save the captcha.php, we will learn how to implement it in a web form. So, we need to create another PHP script. Let’s name it with test.php and place it in the same folder as captcha.php.
[php]<?
session_start();
if(isset($_POST['cmd_verify']) && isset($_POST['txt_captcha'])){
$verify_code = $_POST['txt_captcha'];
if(md5($verify_code) === $_SESSION['verify_key']){
//verified
echo "<b>You have been verified</b><br /><br />";
//You can place more code here
}else{
//not verified
echo "<b>Wrong verification code</b><br /><br />";
}
}
?>
<form action="test.php" method="post">
Please type in the verification code printed on this image
<br /><br />
<img src="captcha.php"> <br /><br />
Type the code here <input type="text" size="10" name="txt_captcha">
<br /><br />
<input type="submit" value="Submit" name="cmd_verify" style="width:100px">
</form>[/php]
The coding part is complete. The last thing to do is to try it. Good luck!
Copyright Note
This script is free to use but please do not remove the comment in the script.
Related Links
What is CAPTCHA?
PHP
PHP:GD Manual
GD Graphics Library
GD Library






Um not working dude
And my GD library is enabled
I test it locally and online as well
Plus, when you write a tut would be nice if you show a live working demo/example as well as give some info like for which verion of php you wrote the script etc…
Goog luck with your projects
Thanks for your suggestion. It may be caused by you have not provide and set the background files.