My captcha script can be implemented in two steps:
Step1: Code for HTML Form
In the first step you need to enter a piece of code in your html form, in order to make the implementation of this captcha script easier I am using a dummy HTML form. The code is as follows:
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <table width="100%" border="0"> <tr> <td>Select Name:</td> <td><label> <input type="text" name="test1" id="test1" /> </label></td> </tr> <tr> <td>Select Email:</td> <td><label> <input type="text" name="test2" id="test2" /> </label></td> </tr> <tr> <td colspan="2">Spam Check <?php $num1=rand(1,9); //Generate First number between 1 and 9 $num2=rand(1,9); //Generate Second number between 1 and 9 $captcha_total=$num1+$num2; echo $num1." + ".$num2; ?>= <label> <input name="captcha_entered" type="text" id="captcha_entered" size="5" maxlength="2" /> </label><input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $captcha_total; ?>" /></td> </tr> <tr> <td colspan="2"><label> <input type="submit" name="button" id="button" value="submit" /> </label></td> </tr> </table> </form>Step 2: Verifying the entered captcha
In the second step you need to verify the information entered in the captcha. You can easily understand the verification process from the following code:
<?php // Checking if the form is being submitted if ($_REQUEST['button']=="submit") { //Checking if the captch entered is correct if ($_REQUEST['captcha_entered']==$_REQUEST['captcha_total']) { //Place your code for processing the form here echo "test"; } else { //Place here the error message to be displayed echo "<script>alert('Spam Check Failed');</script>"; } } ?>Well guys thats all you need to do to create your own captcha. You can find the complete captcha script below:
<?php // Checking if the form is being submitted if ($_REQUEST['button']=="submit") { //Checking if the captch entered is correct if ($_REQUEST['captcha_entered']==$_REQUEST['captcha_total']) { //Place your code for processing the form here echo "test"; } else { //Place here the error message to be displayed echo "<script>alert('Spam Check Failed');</script>"; } } ?> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <table width="100%" border="0"> <tr> <td>Select Name:</td> <td><label> <input type="text" name="test1" id="test1" /> </label></td> </tr> <tr> <td>Select Email:</td> <td><label> <input type="text" name="test2" id="test2" /> </label></td> </tr> <tr> <td colspan="2">Spam Check <?php $num1=rand(1,9); //Generate First number between 1 and 9 $num2=rand(1,9); //Generate Second number between 1 and 9 $captcha_total=$num1+$num2; echo $num1." + ".$num2; ?>= <label> <input name="captcha_entered" type="text" id="captcha_entered" size="5" maxlength="2" /> </label><input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $captcha_total; ?>" /></td> </tr> <tr> <td colspan="2"><label> <input type="submit" name="button" id="button" value="submit" /> </label></td> </tr> </table> </form>If you have any queries or suggestions please feel free to comment.
No comments:
Post a Comment