Responsive image

Call Us Today! 305 434 4698

Email : info@bestwebsitesdesigner.com

Banner Image2
Recent Articles
Search Article
Article Category
How To Add Numeric Calculation Captcha In Website
Friday, April 5, 2013    10:11 am

Most of Web Designer face a issue when they create a new website for their clients. This issue is that the website they have created is automatically sending 100 of forms to the client email address. Even though some fields are made required still clients get 100 of emails.

Solution to this is to add a Captcha to your Website. Most of Website Developer add a image based Captcha. But this Captcha sometimes make it very difficult for the clients to read the text in image based captcha which results in losing of good lead. Text based Captcha can be used but bots can easily read them and fill the form easily.

To solve this we can use calculation based text Captcha. As spam bots cannot do the calculation this method is very useful. Only disadvantage is if complex addition or subtraction is shown than clients may have to spend some time to submit the Captcha.

This Article is going to show you how to add numeric calculation captcha in the website or contact form.

 

Please Follow The Procedure

Step 1 : If Your Website Is in Php then this code will work perfectly and for HTML website just SAVE your contact page(HTML) or the page where you want to add captcha in .PHP form.

Step 2 : On the very first line above the <!DOCTYPE html PUBLIC “—-”> Code just Paste this code

<?php
ob_start(“ob_gzhandler”);
session_start();
$digit1 = mt_rand(1,20);
$digit2 = mt_rand(1,20);
$math = “$digit1 + $digit2″;
$_SESSION['answer'] = $digit1 + $digit2;

?>

This Code Will Generate 2 Random Numbers And Do The Calculation Of Them.

Step 3 : Add The Following Code Where You Want To Display Captcha.

(If You want to add captcha in the contact form so put the code on above the save button’s code

(Like : <input type=”submit” name=”submit”  />) // this is save button code.)

<input type=”hidden” value=”<?php echo $_SESSION['answer'] ?>” name=”hdn” />
What’s <?php echo $math; ?> = <input name=”answer” type=”text” />

Here value=”<?php echo $_SESSION['answer'] ?>” is the answer Which is pass to the hidden field.

And <?php echo $math; ?> is the random value generated by the function.

Step 4 :  To check if the field for captcha is blank or entered value is wrong write the following javascript code using function and call that function on onsubmit event of the form by writing onsubmit=”return validate();” in the form tag

(Like :<form action=”contact_email.php” id=”" method=”post” name=”contact” enctype=”multipart/form-data” onSubmit=”return validate();”>)

Here Validate is The Name Of Function.

<script language=”javascript”>
function validate()
{

var x=document.forms["contact"]["answer"].value;
if (x==null || x==”")
{
alert(“Please Enter Security Code.”);
document.contact.answer.focus();
return false;
}

var x=document.forms["contact"]["answer"].value;
var y=document.forms["contact"]["hdn"].value;
if(x!=y)
{
alert(“Error! The Codes Do Not Match”);
document.contact.answer.value=”";
document.contact.answer.focus();
return false;
}
}
</script>

Step 5 : If Because of any reason your Javascript Not load then you can add this code (For More Safety Purpose ) in your mail.php or contact_email.php etc. in which you wrote the code for sending mail just put these lines of code from the beginning of page.

<?php

ob_start(“ob_gzhandler”);
session_start();
$b=$_POST['answer'];
$c=$_POST['hdn'];
if($b==$c)
{
// Code to send Email
}
else
{
Display Failure page.
}
?>

 

To understand it more properly You Can watch the video

By: admin

[Go Back]