Wednesday, 8 October 2014

Simple Captcha Code in PHP

<?php
$capfail=false;
 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'];
 $date=date('Y-m-d');

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

<tr><td></td>
 <td><?php echo $capt1.'+'.$capt2; ?></td></tr>
 <?php
 if((isset($_POST['submit']))&&($_POST['vercode']!=$_POST['captcha'])){
 echo "<div class='error'> <p>Enter valid captcha!</p></div>";
 $capfail=true;

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

<?php } ?>

0 comments:

Post a Comment