Captcha humanizado
Posted on | setembro 29, 2008 | No Comments
Para quem não sabe o que é captcha, em linhas gerais são aquelas palavras, letras ou conjunto de letras/números que verificam se quem está enviando a informação para outra pessoa é uma pessoa mesmo.
Pensando num meio de fazer isso de modo mais acessível, utilizei as idéias de Ed Eliot para fazer um script em PHP que fosse mais amigável também para quem tem visão subnormal ou de alguma forma utiliza softwares que lêem caracteres em tela.
É uma versão bem básica e simplificada, precisa ser aprimorada, mas funciona.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /** * Class humanized captcha (hc) * captcha making sense in forms * */ session_start(); class hc{ var $question; var $answer; var $options; var $options_html; var $session; var $hash_answer; public function __construct($question = NULL, $answer = NULL, $options = NULL) { $this->session = "humanized_captcha"; //uniqid(md5(microtime()), true); $this->answer = $answer; $this->options = $options; $this->question = $question; $_SESSION["hc"] = $this->session; $_SESSION[$this->session]["question"] = $question; $_SESSION[$this->session]["answer"] = $answer; $_SESSION[$this->session]["options"] = $options; $_SESSION[$this->session]["hash"] = uniqid(md5(microtime()), true); print_r($_SESSION); } public function show() { $this->options_html[] = $this->answer; foreach($this->options as $opt) { $this->options_html[] = $opt; } shuffle($this->options_html); print_r($this->options_html); $html[] = "<span id=\"captcha_question\">" . htmlentities($this->question) . "</span>\n<div id=\"captcha_answers\">\n"; foreach($this->options_html as $ans) { // (($ans == $this->answer)? ($_SESSION[$this->session]["hash"]) // (uniqid(md5(microtime()), true)) $html[] = "<span class=\"captcha_option\"><label> <input type=\"radio\" name=\"captcha_option\" value=\"" . (($ans == $this->answer)? ($_SESSION[$this->session]["hash"]) : (uniqid(md5(microtime()), true)) ) . "\" />" . htmlentities($ans) . "</label></span>\n"; } $html[] = "</div>"; return implode("", $html); } public function validate() { $return = empty($_GET["captcha_option"])? $_POST["captcha_option"] : $_GET["captcha_option"]; if ($return != $_SESSION["humanized_captcha"]["hash"]) { echo "Invalid code"; } else { echo "Valid code"; } } public function __destroy() { unset($_SESSION[$this->session]); } } |
Você deve usar essa classe dessa forma:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | include_once("captcha.class.php");
<style>
#captcha_question, #captcha_question, .captcha_option
{
display: block;
}
</style>
<form action = "form01.php" method="post">
<?php
if(!isset($_GET["captcha_option"]) & !isset($_POST["captcha_option"])){
$hc = new hc("Qual o nome do mamífero", "hipopótamo", array("vermelho", "cômodo", "vértebra", "educação"));
echo $hc->show();
} else {
hc::validate();
}
?>
<input type="submit">
</form> |
Category: Acessibilidade, Orientação a Objetos, PHP, Programação
Tags: Acessibilidade > captcha > classe > OOP > PHP
Tags: Acessibilidade > captcha > classe > OOP > PHP
Comments
Leave a Reply
