5
I have a captcha in a form where an image with 5 randomly generated values is displayed. How do I validate the captcha and only send the contact if the input value is equal to the one generated by the image?
Excerpt from the form that has the captcha:
<div class="col one-fourth" style="padding: 0 3px;">
<img src="captcha.php"/>
</div>
<div class="col one-fourth" style="padding: 0 3px;">
<input type="text" name="captcha" id="captcha" placeholder="Digite o código ao lado" class="campos_form" maxlength="5" required/>
</div>
This is the file I call that generates the image with captcha:
<?php
session_start();
$codigoCaptcha = substr(md5( time()) ,0,5);
$_SESSION['captcha'] = $codigoCaptcha;
$imagemCaptcha = imagecreatefrompng("imagens/fundocaptcha.png");
$fonteCaptcha = imageloadfont("anonymous.gdf");
$corCaptcha = imagecolorallocate($imagemCaptcha,46,139,87);
imagestring($imagemCaptcha,$fonteCaptcha,15,5,$codigoCaptcha,$corCaptcha);
header("Content-type: image/png");
imagepng($imagemCaptcha);
imagedestroy($imagemCaptcha);
?>
How do I check if the user entered correctly?
I can use the $_SESSION['captcha']
to compare the value? And how do I capture the value of input
?
Hi, @PHP, you don’t need to tag the title, only if it’s organic. Usually it is better to describe the problem, the site’s regulars already filter the questions they want to answer using the tag system that [pt.so] offers.
– brasofilo