How to Generate Captcha Image in PHP ?
What Does Captcha Stand For?
A Captcha is a type of challenge-response test used in the website as an attempt to ensure that the response is generated by a human being. A Captcha is one kind of program that can generate and also test that humans can pass but computer programs cannot. Say, for example, humans can read distorted text, but current computer programs can't:
CAPTCHA means "Completely Automated Public Turing test to tell Computers and Humans Apart".
Types of Captcha
There are basically 3 types of captcha. They are as below:
- Image Captcha
- Mathematical Captcha
- Voice Captcha
Uses of Captcha
- To Prevent Comment Spam in Blogs
- Website Registration Protection
- Online Polls
- To Prevent Dictionary Attacks
- Search Engine Bots
- Email Worms and Spam
Among all the types as given above, image captcha is the most popular and widely used. So, In this tutorial, we will learn how to generate and use image captcha using PHP. This tutorial is divided into mainly three parts. We will see them one by one.
Image Captcha Generation
Here is the PHP code to generate images with random code. Save it as captcha.php
.First start a session to store captcha code in session then generate random code using the PHP rand()
function and write it on the generated image.
<?php
session_start();
$ResultStr = '';
for ($i = 0; $i < 5; $i++) {
$ResultStr .= chr(rand(97, 122));
}
$NewImage = imagecreatefromjpeg("img.jpg");
$LineColor = imagecolorallocate($NewImage,233,239,239);
$TextColor = imagecolorallocate($NewImage, 127, 127, 127);
imageline($NewImage,1,1,40,40,$LineColor);
imageline($NewImage,1,100,60,0,$LineColor);
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);
$_SESSION['key'] = $ResultStr;
header("Content-type: image/jpeg");
imagejpeg($NewImage);
?>
Image Captcha Integration in HTML
Save the below file as index.php
.
<?php
session_start();
?>
<html>
<form name="post" method="post" action="post.php">
<div class="captcha_example">
<div class="top"><p>Captcha generation in PHP | PHP Dev Zone</p></div>
<div class="inner">
<div>
<div style="margin-bottom:10px;">
<h4>Captcha image:</h4>
<img src="captcha.php" class="form_captcha" />
<div class="lines">Verification (Type what you see):</div>
<input name="number" type="text" class="captcha">
<input name="Submit" type="submit" class="main-color-bg" value="Submit">
</div>
<p>Here captcha image is being generated using GD library, each time when page loads it display different combinations of symbols.</p>
<h2 style="float: right;"><a href="https://www.php-dev-zone.com/post/how-to-generate-captcha-image-in-php">Back to Article</a></h2>
</div>
</div>
</div>
</form>
</html>
Image Captcha Response Validation
First, start a session to access $_SESSION[“key”]
variable then compare it with posted captcha response. If both are the same then run your code else exit from code with message Wrong Code Entered. Save below file as post.php
<?php
session_start();
if (isset($_REQUEST['Submit'])) {
$number = $_REQUEST['number'];
$key = $_SESSION['key'];
if ($number!=$key) {
echo ' Wrong Code Entered! Please try again!';
} else {
echo ' Your string is valid!';
//You can make query to insert the data into database here.
}
}
?>
That’s it. Simple Captcha Generation in PHP is Completed.