I want to get the session captcha value in the onsubmit event of the form , so if the text entered is different of the session captcha data then I will cancel form submission. The problem is that I cannot get the session captcha value on document ready :
file of the captcha generation is named securitecode.php :
<?php
session_start();
$largeur = 120;
$hauteur = 40;
$longueur = 5;
$liste = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$code = '';
$counter = 0;
$image = @imagecreate($largeur, $hauteur) or die('Impossible d\'initializer GD')
;
for( $i=0; $i<10; $i++ ) {
imageline($image,
mt_rand(0,$largeur), mt_rand(0,$hauteur),
mt_rand(0,$largeur), mt_rand(0,$hauteur),
imagecolorallocate($image, mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)
));
}
for( $i=0, $x=0; $i<$longueur; $i++ ) {
$charactere = substr($liste, rand(0, strlen($liste)-1), 1);
$x += 10 + mt_rand(0,10);
imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $charactere,
imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155)));
$code .= $charactere;
}
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['securecode'] = $code;
?>
In my web page I create the image captcha :
...
<img id="captcha_img" src="securitecode.php" />
<input type="text" id="captcha" />
<span id="msg_captcha"></span>
...
<script type="text/javascript">
$(document).ready(function() {
$('#msg_captcha').html("<?php echo $_SESSION['securecode']; ?>");
});
</script>
When the page loads for the first time there is nothing displayed inside the #msg_captcha span ! So how to get the captcha session data ?