Home >>Advance PHP Tutorial >PHP CAPTCHA
<?php error_reporting(1); $arr=range(99,9); $brr=range(99,9); $randa=array_rand($arr); $randb=array_rand($brr); $a=$arr[$randa]; $b=$brr[$randb]; $r=$a+$b; $cap=$a."+".$b; if(isset($_POST['b1'])) { if($_POST['t2']==$_POST['t3']) { echo '<center>'.'<font color="blue" size="5">'."Welcome user".'</font>'.'</center>'; } else { echo '<center>'.'<font color="red" size="5">'."Please fill the correct answer".'</font>'.'</center>'; } } ?> <html> <center> <form method="post"> <?php error_reporting(1); echo $cap."="; ?> <input type="hidden" name="t3" value="<?php echo $r; ?>"> <input type="text" name="t2" autofocus><br> <input type="submit" name="b1" value="match"> </form> </center> </html>
<form action="" method="post" > <table border="0" align="center"> <tr> <td width="215"> Validation code:</td> <td width="162"> <?php $arr= array_merge(range(0,9),range("A","Z")); //print_r($arr); for($i=1;$i<=5;$i++) { $ch = $arr[array_rand($arr)]; @$captcha=$captcha.$ch; @$fc=$fc.$ch.","; } //echo $fc."<br>"; $nar = explode(",",$fc); for($i=0;$i<5;$i++) { echo $nar[$i]; //echo "<img src='$nar[$i].GIF'/>"; } if(isset($_POST['match'])) { if($_POST['img']==$_POST['hid']) { echo "<br/><font color='blue'>security code matched</font>"; } else { echo "<br/><font color='red'> try again</font>"; } } ?> <tr> <td>Enter the above code here :</td> <td> <input name="img" type="text"> </td> </tr> <tr> <td colspan="2" align="center"> <input name="match" type="submit" value="Submit Security code"></td> </tr> </table> <input type="hidden" value="<?php echo $captcha; ?>" name="hid"/> </form> </pre> </div><br/>