Techumber
Home Blog Work

Simple Captcha With PHP

Published on September 6, 2012

Captcha! When you sign up for some web site or when you post some comment on blog, you might see one extra field where you need to fill it with the character from an image over there. Do you ever think why is this for. Captcha is used as antispam plugin. If no Captcha for your webSite signup page there is change that number of Span bots may fill your database with junk data. In today’s PHP Tutorial Let’s create a simple php captcha script. It is totally free PHP Captcha Script you can download it from below download link.  create simple php captcha script free download

Demo

Download

Steps

1)we will generate a random string and store it in php session. 2)We will generate a Image with help of php ImageCreate function. On that Image will put the random string we had generated. 3)Finally we will show that image to user when user enter data we will check it againest data which we stored in php session.

index.php

<?php
session_start();
if($_POST){
if (empty($_SESSION['security_code']) || trim(strtolower($_REQUEST['security_code'])) != $_SESSION['security_code']){
$msg= 'Please enter valid text';
}
else{
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name=htmlentities($_POST['inputEmail']);
$message=htmlentities($_POST['inputMessage']);
// Insert SQL Statement
$msg= 'Success!';
}
}
unset($_SESSION['captcha']);
}
?>
<html>
<body>
<form class="form-horizontal mini-layout" method="post" action="">
<legend>Contact</legend>
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="text" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputMessage">Message</label>
<div class="controls">
<textarea type="text" id="inputMessage" placeholder="Type message "></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="captcha">Captcha</label>
<div class="controls">
<input type="text" id="security_code" name="security_code" placeholder="Enter below code">
</div>
<div class="controls">
**<img src="captcha.php" alt="captcha"/>**
</div>
<div class="controls">
<a href="">Not Readable?</a>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</body>
</html>

In above markup observer that we have used captcha.php as image tag source. That means our captcha.php will generate an image and display it over there.

captcha.php

<?php
session_start();
//Creating captcha object
$capt = new Captcha();
$capt->createImage();
exit();
//Create Captcha class
Class Captcha{
//fonts we use
public $fonts = array('AntykwaBold','Candice','Ding-DongDaddyO' ,'Duality','Heineken','Jura','StayPuft','TimesNewRomanBold' ,'VeraSansBold',);
//1.generateing text
function getText(){
//Generateing random lenght of text betweetn 4 to 7
$length = rand(4,7);
$alpha = "abcdefghijlmnopqrstvwyz";
$voc = "aeiou";
$text = "";
$vocal = rand(0, 1);
for ($i=0; $i<$length; $i++) {
if ($vocal) {
$text .= substr($voc, mt_rand(0, 4), 1);
}else {
$text .= substr($alpha, mt_rand(0, 22), 1);
}
$vocal = !$vocal;
}
return $text;
}
//Creating image
function createImage(){
$security_code =$this->getText();
//setting session
$_SESSION["security_code"] = $security_code;
$width = 198;
$height = 40;
//colrs
$colors = array(
array(27,78,181), // blue
array(22,163,35), // green
array(214,36,7),
array(173,71,178), // red
);
$rc=rand(0,3);
//font location
$font="fonts/".$this->fonts[array_rand($this->fonts)].".ttf";
$image = ImageCreate($width, $height);
$white = ImageColorAllocate($image, 255, 255, 255);
$textcolor = ImageColorAllocate($image, $colors[$rc][0], $colors[$rc][1], $colors[$bc][2]);
$grey = imagecolorallocate($image, 128, 128, 128);
ImageFill($image, 0, 0, $white);
imagettftext($image, 20, 0, 11, 21, $grey, $font, $security_code);
imagettftext($image, 20, 0, 10, 30, $textcolor, $font, $security_code);
header("Content-Type: image/jpeg");
ImageJpeg($image);
ImageDestroy($image);
}
}
?>

That’s it hope you like it.