3
I need a very simple captcha, but it can be validated before the form is sent, how can I do it?
Currently use this:
index.html
<form method="post" action="valida.php" id="form" name="form">
<!-- campos -->
<div class="form-group">
<label>Captcha*:<img src="captcha.php?l=105&a=35&tf=15&ql=5"></label>
<input type="text" name="palavra" id="palavra" class="form-control input-lg" required>
</div>
</form>
captcha.php
<?php
session_start();
header("Content-type: image/jpeg");
function captcha($largura,$altura,$tamanho_fonte,$quantidade_letras){
$imagem = imagecreate($largura,$altura);
$fonte = "verdana.ttf";
$preto = imagecolorallocate($imagem,243,243,243);
$branco = imagecolorallocate($imagem,0,151,182);
$palavra = substr(str_shuffle("ABCDEFGHJKLMNPQRSTUVYXWZ23456789"),0,($quantidade_letras));
$_SESSION["palavra"] = $palavra;
for($i = 1; $i <= $quantidade_letras; $i++){
imagettftext($imagem,$tamanho_fonte,rand(0,0),($tamanho_fonte*$i),($tamanho_fonte + 10),$branco,$fonte,substr($palavra,($i-1),1));
}
imagejpeg($imagem);
imagedestroy($imagem);
}
$largura = $_GET["l"];
$altura = $_GET["a"];
$tamanho_fonte = $_GET["tf"];
$quantidade_letras = $_GET["ql"];
captcha($largura,$altura,$tamanho_fonte,$quantidade_letras);
?>
valida.php
if ($_POST["palavra"] == $_SESSION["palavra"]) {
//...
}
but I don’t know how to check if the captcha was filled out correctly before sending the form.
With that
if
that has down does not do that?– Miguel
yes, but to get there the form has already been sent, I wanted him to check before sending. It’s @Miguel?
– Raylan Soares
The problem is the definition of "already sent". " No" there is how you know if it is filled out correctly (or not), without sending a request to the server. What you can do is create an ajax for when the user type send a request to the server. Then return to javascript if it is correct or not. If it is correct the user can allow the user to submit the form. I would advise using Google Recaptcha, for such purpose, anyway.
– Inkeliz
I get it, but is there no way to compare what the guy is typing with what’s in session @Inkeliz? I thought about it but I don’t know if it would work or how it would work.
– Raylan Soares
It exists, since it uses Javascript for this purpose. But what would be the real purpose of checking the captcha before sending the form? I believe there is some other solution, better than making two requests, one just to check the captcha.
– Inkeliz
Is that the form is integrated with an external tool to compute the data sent and this is done at the time of sending the form, so if the captcha is right or not it will compute anyway if I have to send to check.
– Raylan Soares
Use recaptcha on google, between https://www.google.com/recaptcha/intro/index.html is easier for free and easy to do tutorial.
– KingRider