Captcha Code in PHP

For all web developers and freelancers it’s a useful post because they have to code a registration page for almost all projects . It is always recommended to put some captcha verification mechanism in registration pages in order to prevent spams and bots. Here I am going to share PHP solution for captcha generation and verification. Its very easy to implement captcha verification in PHP if you understand the logic. Here is the code for captcha generation and verification:

You can check LIVE DEMO HERE



 function capt1(){
 $val1 = rand(0,9);
 return $val1;
 function capt2(){
 $val2 = rand(0,9);
 return $val2;
 if((isset($_POST['submit']))&& $capfail==false){
 $name= mysql_real_escape_string(trim($_POST['uname']));
 $email = mysql_real_escape_string(trim($_POST['email']));
 $mobile = mysql_real_escape_string(trim($_POST['contact']));
 $message = $_POST['comments'];

 <?php if((!isset($_POST['submit']))||($_POST['vercode']!=$_POST['captcha'])){?>
 <form action="captcha.php" method="POST">
 <td>Enter Your Name:</td>
 <td><input type="text" name="uname" /></td>
 <td><input type="text" name="email" /></td>
 <td>Contact Number:</td>
 <td><input type="text" name="contact" /></td>
 <td><textarea name="comments" type="textarea" /></textarea></td>
 <input type="hidden" name="captcha" id="captcha" value="<?php $capt1=capt1();
 $capt2=capt2(); echo $addcapt=$capt1+$capt2;?>"/>
 <td>Enter Sum</td>
 <td><input type="text" name="vercode" id="vercode" /></td>

 <td><?php echo $capt1.'+'.$capt2; ?></td></tr>
 echo "<div class='error'> <p>Enter valid captcha!</p></div>";

} ?>
 <input type="hidden" name="rand" id="rand" value="<?php echo rand(); ?> "/>
 <tr><td> <input type="submit" name="submit" value="Send Your Message"/>

<?php } ?> 

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *