How to Create CAPTCHA Code in PHP

In this tutorial we will learn how to create CAPTCHA code in PHP. CAPTCHA is basically used to determine either a user is robot or human being. It is used to avoid spam abuse or auto submission of form on website or blog. In this tutorial we are creating CAPTCHA code using image create function of PHP as we have discussed in our previous tutorial.
CAPTCHA code is generated as random string and stored it in session. Session variable which store random string write to an image to generate a CAPTCHA Code with image. When form tends to submit then checked for CAPTCHA code verification. This is the simple and easy concept to generate CAPTCHA code with image in PHP.

How to Create CAPTCHA Code in PHP
How to Create CAPTCHA Code in PHP

Demo
captcha.php
Here first of all we unset session variable which have CAPTCHA code so that not repeat same value. We generate a random string and store it in session variable. This session variable is passed to imagestring() function to write Session variable value(CAPTCHA Code) to an image.

<?php
if(isset($_SESSION['captcha']))
{
unset($_SESSION['captcha']); // destroy the session if already there
}
session_start();
$string1 = "abcdefghijklmnopqrstuvwxyz";
$string2 = "1234567890";
$string = $string1.$string2;
$string = str_shuffle($string);
$rand = substr($string,0,5);
$_SESSION['captcha']=$rand;

/* Create an image in php */
$myimg = imagecreate(100, 30);
$background = imagecolorallocate($myimg, 199, 144, 225); // background color
$text_colour = imagecolorallocate ($myimg, 245, 245, 245);      // text color 
imagestring($myimg,12,25,8,$_SESSION['captcha'],$text_colour); // add captcha code in image 
header ("Content-type: image/png");
imagepng ($myimg); // displayed image createde
imagedestroy($myimg); // remove allocated memory. 
?>

index.php
This page contains HTML form to enter Captcha code and PHP code to verify Captcha Code is is correct or not. Captcha code load with image by passing src as “captcha.php” in “img src”.

<?php 
session_start();
if(isset($_POST['submit'])) 
{
if($_SESSION['captcha']==$_POST['cap'])
{
	echo "success";
}
else
{
echo "Captch verification failed! Try again"."<br>";
echo "Text is:".$_SESSION['captcha']; 
}
}
?>
<form method="POST" action="index.php">
<input type="text" name= "cap" >
<input type="submit" name="submit">
</form>
<img src="captcha.php" id="captc">
<span onClick="changetext();"><img src="reload.png" /></span>

JavaScript
Using JavaScript we change Captcha code by onClick on reload image. Each time by onClick on reload image loads a new Captcha code from captcha.php in img src.

<script type="text/javascript">
function changetext()
{
var img = document.getElementById("captc");
img.src="captcha.php";
}
</script>

Demo

This one is simple and easy approach to generate CAPTCHA Verification Code. If you liked this article 🙂 please share with your friends and colleagues 🙂 . If you have any query please write in comment.

One thought on “How to Create CAPTCHA Code in PHP

Leave a Reply

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