Wednesday 27 July 2016

How to create Captcha using PHP..?

How to create Captcha using PHP..?

Step 1: First of all create the function for random numbers
               <?php
                      function randomString($len=4){
                        $validChar="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()";
$validCharNumber=strlen($validChar);
$result="";

for($i=0;$i<$length;$i++){
$index=mt_rand(0,$validChar-1);
$result .= $validChar[$index];
}
return $result;
}
?>

Step 2: Now Write the Script for the captcha
<script>
function checkCaptcha(name1,name2){
if(name1!=name2){
alert("Security code incorrect or left blank, Please enter correct security code carefully....!");
return false;
}else{
return true;
}
if(!chkcaptcha(jj.value,captcha.value)
 
)
{
return false;
}
}
</script>

Step3: Now Create the HTML Fom for the Captcha

<form>
<table>
<tr>
<td>Enter the Captcha</td><td><input name="jj" type="text" disabled="disabled" class="dropdown" id="jj"  style="background-image:url(images/captcha2.png); text-align:center; color:green; font-size:14px; size:15;   font-weight:bold; letter-spacing:2px;" value="<?php echo getRandomString(); ?>" size="10" readonly/>
    <input name="captcha" type="text" style="color:#000;" id="captcha"  size="10" maxlength="4" /></td>
</tr>
</table>
</form>