Hola amigo tengo un script echo en php pero no me gusta, creo que es muy lento y aporta muy copas cosas, quisiera algo simple, yo antes creaba una variable aleatoria con caracteres alfanuméricos y de esa forma validaba mis formularios pero no se si es una buena practica, pero antes de usar un montón de código para tener casi el mismo resultado prefiero seguir con la variable aleatoria
index.php
Código PHP:
<? session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<? if($_GET['sendResponse'] == "true" ){
if(md5($_POST['cpVal']) == $_SESSION['keyId']){
echo "Sucesso";
}else{
echo "Falhado";
}
}
?>
<form action="?sendResponse=true" method="post">
<p>
<img src="captcha.php" />
</p>
<p>
<input name="cpVal" type="text" size="5" maxlength="5" />
<label>
<input type="submit" name="btSend" id="btSend" value="Validar" />
</label>
</p>
</form>
</body>
</html>
captcha.class.php
Código PHP:
<?
session_start();
class cbCaptcha {
private $numChars;
private $im;
public $generatedCode;
private $altura,$largura;
// Function cria imagem
public function CreateImage(){
//CharacterGenerator();
$this->im = imagecreate($this->largura,$this->altura);
$colors = array(imagecolorallocate($this->im,255,193,193), imagecolorallocate($this->im,238,64,0), imagecolorallocate($this->im,255,165,0));
$_SESSION['keyId'] = md5($this->generatedCode);
$cpreto = imagecolorallocate($this->im,0,0,0);
$cBranco = imagecolorallocate($this->im,255,255,255);
imagefill($this->im,0,0,$cBranco);
imagettftext($this->im,25,1,0,$this->altura / 2,$cpreto,"Jura.ttf",$this->generatedCode);
//imagestring($this->im,5,$this->largura / 2 ,$this->altura/2,$this->generatedCode, $cpreto);
imagejpeg($this->im);
//imagedestroy($im);
}
// Construtor da classe ///////////////////////////////////
public function __Construct($qtdeChars = 1, $width = 200, $height = 70){
$this->numChars = $qtdeChars;
$this->largura = $width;
$this->altura = $height;
$this->generatedCode = $this->CharacterGenerator($qtdeChars);
$this->CreateImage();
}
///////////////////////////////////////////////////////FIM
// function gera codigo
//////////////////////////////////////////////////////////////////////////////////////////////////
public function CharacterGenerator($valor){
$pal = "qwertyuiopasdfghjklzxcvbnm1234567890";
$pal_len = strlen($pal);
$aux[$valor];
$scramble[$valor];
for($i = 0; $i < $valor; $i++){
$scramble[$i] = substr($pal,floor(rand(5,$pal_len) +$i),1);
}
$string_f = implode('',$scramble);
return $string_f;
}
//////////////////////////////////////////////////////////////////////////////////////////FIM
} // fim classe
?>
captcha.php
Código PHP:
<?
require_once("captcha.class.php");
header("Content-Type: image/jpeg");
$cap = new cbCaptcha(5,150,70);
//$cap->CharacterGenerator(5);
?>
Demo:
http://jame.net16.net/
Fonts :
http://jame.net16.net/Jura.ttf
Pienso que esta muy mal echo pero vamos funciona que es lo importante y se agradece pero no me termina de convencer si alguno es capas de optimizar o ver cual es el problema que tiene seria de gran ayuda y me ahorraría mucho tiempo, yo ahora estoy probando cosas para ver que se puede hacer
por si no me comprendéis yo quiero un captcha simple muy simple pero efectivo