Autor Tema: Tutorial de Captcha (Verificacion de formularios)  (Leído 1438 veces)

Desconectado Snead

  • PHPer@ Fijo
  • ***
  • Mensajes: 128
  • Karma: 2
  • Snead [Security;n'Design]
    • Ver Perfil
    • Diseño web
Tutorial de Captcha (Verificacion de formularios)
« en: 11 de Diciembre de 2007, 21:59:07 pm »
Bueno, como saben hay muchisimas webs qe cuando quieres enviar un formulario, registrarte o lo qe sea, te pide que copies el texto de una imagen en un TEXTBOX. Esto sirve para evitar spam (qe te envien miles de mensajes con algun programa). Bueno no muchos saben hacer un captcha, es por eso qe en este tutorial voy a enseñar a hacer uno :D

Primero el FORM.HTML donde tomamos los datos.

<pFORM PARA ENVIAR DATOS CON VERIFICACION ANTI-BOTSPAM </p>
<
form method="POST" action="procesar_consulta.php">
Nombre: <input type="text" name="nombre"><br>
Mensaje: <input type="text" name="mensaje"><br>
Imagen de verificacion:<br>
<
iframe name="img" height="60" width="200" scrolling="no" border="0" frameborder="0" src="img.php">
Usa otro navegador</iframe><br> <!-- IFRAME QE MUESTRA LA IMAGEN -->
Verificacion: <input type="text" name="verificacion"><br>
<
input type="submit" value="Enviar" name="enviar">
</
form>



Segundo, el programa php que crea la imagen aleatoria: IMG.PHP

<?
header("Content-type: image/png");

$fondo "fondo.PNG";
$imagen imagecreatefromPNG($fondo);
$caracteres "1234567890abcdefghijklmnopqrstuvwxyz"//CARACTERES QE CONTIENE
$digitos="6";

	
for(
$i=0;$i<$digitos;$i++)
	
{
	
$aleatorio .= $caracteres{rand(0,35)};
	
}

//VARIABLES DE LA IMAGEN ALEATORIA
$rojo rand (200255);
$verde rand (200255); // ELEGIMOS COLORES CLAROS PORQE EL FONDO ES NEGRO
$azul rand (200255);
$x rand (2050);
$y rand (1823);
$angulo "0";
$letra "tuletra.ttf"// EL ARCHIVO TTF DEL TIPO DE LETRA QE VAS A USAR
$color imagecolorallocate ($imagen,$rojo,$verde,$azul);
$size rand(10,12);
$nombre_img="$aleatorio.PNG";

imagettftext ($imagen$size$angulo$x$y$color$letra$aleatorio);

imagepng ($imagen);

imagepng ($imagen$nombre_img);
    
session_start();
$_SESSION[&#39;verificacion&#39;] = $aleatorio; //CREAMOS UNA SESSION QUE CONTENGA EL TEXTO DE LA IMAGEN

imagedestroy ($imagen);
?>


Por ultimo el PROCESAR_CONSULTA.PHP
que hace lo que queramos con el form, unicamente si la verificacion es correcta.


<?
$nombre=$_POST[nombre];
$mensaje=$_POST[mensaje];
$verificacion=$_POST[verificacion];
session_start();
          if (
$_SESSION[&#39;verificacion&#39;] == $verificacion)
	
{
// HACER LO QE QIERAS CON LOS DATOS DEL FORM!
?>
<p align="center">Mensaje enviado</p>
<?
	
}
	
else
	
{
?>
<p align="center">La imagen no coincide con la verificacion</p>
<?
	
}
unlink ("$verificacion.PNG"); // BORRAMOS LA IMAGEN PARA QE NO NOS OCUPE ESPACIO
?>


En donde hosteen este captcha debe haber los siguientes archivos:
  • Form.html
  • Img.php
  • Procesar_consulta.php
  • tuletra.ttf (es la fuente que vas a usar para la imagen de la verificacion)
  • fondo.PNG (debe ser de 111x30 pixeles y fondo oscuro)

Si alguien no entiende algun paso, se lo explicaré con gusto :D

Saluditos, Snead.

<? Snead (Security;n'Design); ?>

Comunidad PHPeros

Tutorial de Captcha (Verificacion de formularios)
« en: 11 de Diciembre de 2007, 21:59:07 pm »