I am new to web development and I am trying to put CAPTCHA into my website. I am stuck at this. And I couldn't find any help.
The following is my Form code:
<tr>
<td>
<img src="html-contact-form-captcha/captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
</td>
</tr>
<tr>
<td align="right"><b> Enter Image Text </b></td>
<td><input placeholder="Enter the code above here" id="6_letters_code" name="6_letters_code" type="text"><br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
</td>
</tr>
And on this same page I am trying to validate this CAPTCHA by the following code:
var cde = document.getElementById('6_letters_code');
if( cde.value == "" ||
($_SESSION['6_letters_code'] != $_POST['6_letters_code']) ) {
alert( "Code Matched!" );
//alert( "Code Doesn't Match! \n Code Not Entered!" );
return false;
}
And this is where I am getting my CAPTCHA: (captcha.php)
session_start(); // Staring Session
$captchanumber = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz'; // Initializing PHP variable with string
$captchanumber = substr(str_shuffle($captchanumber), 0, 6); // Getting first 6 word after shuffle.
$_SESSION["code"] = $captchanumber; // Initializing session variable with above generated sub-string
$image = imagecreatefromjpeg("bj.jpg"); // Generating CAPTCHA
$foreground = imagecolorallocate($image, 175, 199, 200); // Font Color
imagestring($image, 5, 45, 8, $captchanumber, $foreground);
header('Content-type: image/png');
imagepng($image);
Any help would be appreciated.
Thank you in advance.
$captchanumberput it in a session. Then if the client enters it, the posted captcha number gets encrypted on server side and gets compared with the session.$_SESSIONand$_POST) in Javascript.