Google Like Captcha in PHP
Hello Guys ! Captcha is very important to stop and prevent bots accessing our web forms.as php is widly use i decided to make a class which will generate google like capthca’s in your web pages or web forms.This class is quite interesting as it comes with many customization options.you just need to use this class and some of its resource files.it is quite easy to implement.
Google like PHP captcha comes with lots of advanced customization’s.you can control size,color,background even control the format of the image through class member function we’ll see in the advanced section of this article at the bottom .SO lets move on and see Google Like Capthca working and Full tutorial.
Google Like Captcha in PHP -Live Demo And Download
Lets see the live demo and then download Google like captcha in PHP.
Google Like Captcha in PHP : – Tutorial
lets move to the tutorial of Google like capthca in PHP which comes with many customization options.First we look at what is required to implement Google like captcha in our web Forms
Google Like Captcha in PHP : -required files
- capthca.php <-The captcha class
- image.php <-our image renders in this file
- Resources folder <- contains reuired fonts and libraries.
The Above are the three things required.First thing you should do is to download all these files from link above or at the end of the page and place in your PHP project as such without changing their hierarchy .
Creating web form with Google like Captcha -
Now we Gonna Create a web form in our page say(index.php) .Lets see how do we render google like capthca in our web form.See the code below. Index.php
<form method="post" >
<b>Name</b><br/>
<input type="text" name="name" /><br/>
<b>Message</b><br/>
<textarea name="message"></textarea><br/>
<img src="image.php" id="captcha" /><br/>
<a href="#" onclick="
document.getElementById('captcha').src='image.php?'+Math.random();
document.getElementById('captcha-form').focus();"
id="change-image">Not readable? Change text.</a><br/><br/>
<b>Human Test</b><br/>
<input type="text" name="captcha" id="captcha-form" /><br/>
<input type="submit" />
</form>
this will render the captcha in your webpage(index.php) but not validate it.Lets move on and see validation in our index.php file.validation code will be at the top of index.php
Validating the webform captcha
now we gonna write code to validate the rendered google like captcha.validation code reside at the top of the index.php . index.php(at the top)-
session_start();
$note="";
if (!empty($_REQUEST['captcha']))
{
if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha'])
{
$note= 'Please enter valid text';
}
else
{
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$name=htmlentities($_POST['name']);
$message=htmlentities($_POST['message']);
// Insert SQL Statement
$note= 'Values Inserted';
}
}
unset($_SESSION['captcha']);
}
The code above will validate your captcha.this is how it all works.now lets move to advaced section and see the customization options in this php capthca
Google like Capthca -Advanced Customization : -
Now for the advanced customization we ‘ll open our image .php file.firstly lets see how image.php looks.
Image.php
require_once ('captcha.php') ;
$captcha = new PHPCaptcha();
$captcha->configCaptcha(array(
'width'=>200,//image width -default 200
'height'=>70,//image height-default 70
'quality'=>2,//set quality-high:3 ,mid:2 ,low:1
'blur'=>false,//enablle/disable blur
'format'=>'jpeg',//iage formats. jpeg or png
'wordFile'=>false,//include word file or not
'maxLength'=>5,//min. word length
'minLength'=>8,//max word length
//'background'=>array(87,87,87), //rgb color code here
'color'=>array(
array(128,0,255), //each color code in rgb in form of array
array(0,255,255),
array(255,0,0),
// true //uncomment if overwrite existing colors,comment if append new colors to existing ones
)
)
);
$captcha->CreateImage();
Here we can clearly is Image.php and cutomization done via configCaptcha function of the class PHP captcha.the configCaptcha function is optional for advaced customizations.you can use it to change background color,colors of captcha,format of the image,quality,width,height and length of the captcha string generated in the image.
Google Like Captcha in PHP -Live Demo And Download
Lets see the live demo and then download Google like captcha in PHP.
