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 a robot or a human being.

It is used to avoid spam abuse or auto submission of forms on websites or blogs.

In this tutorial, we are creating CAPTCHA code using the image create function of PHP as we have discussed in our previous tutorial.

CAPTCHA code is generated as a random string and stored in session. Session variable which stores random string write to an image to generate a CAPTCHA Code with an image. When the form tends to submit then checked for CAPTCHA code verification.

This is the simple and easy concept to generate CAPTCHA code with images in PHP.

Demo

captcha.php

Here first of all we unset session variables that have CAPTCHA code so that not repeat the same value. We generate a random string and store it in a 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

[sociallocker]Download[/sociallocker]

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.

1 thought on “How to Create CAPTCHA Code in PHP”

  1. First of all thank you for sharing this informative blog.. this blog really helpful for everyone.. explanation are clear so easy to understand… I got more useful information from this blog

    Reply

Leave a Comment