Ver Mensaje Individual
  #7 (permalink)  
Antiguo 24/09/2010, 03:57
joankass
 
Fecha de Ingreso: septiembre-2010
Mensajes: 8
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Cómo implementar captcha en formulario HTML+PHP

Hola de nuevo.

Ya he encontrado una solución que me funciona, no está nada mal y es sencilla.

La imagen captcha la genero con este código, aunque hay otros que también funcionan: (nombre archivo: captcha:php)

Código PHP:
<?php
 
function randomText($length) { 
$pattern "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for(
$i=0;$i<$length;$i++) { $key .= $pattern{rand(0,62)}; } 
return 
$key


session_start(); 

// PARAMETROS DE LA IMAGEN ////////////////////////////// 
$ancho 100// Ancho de la imágen 
$alto 30// Alto de la imágen 
$lineas 6// Cantidad de lineas de relleno 
$chars 6// Cantidad de caracteres del captcha 

// CREO EL OBJETO IMAGEN Y LOS COLORES A UTILIZAR /////// 
$imagen imagecreate($ancho,$alto); 
$cLineas imagecolorallocate($imagen,140,140,140); 
$cFondo imagecolorallocate($imagen,200,200,200); 
$cTexto imagecolorallocate($imagen,000,000,000); 

// PINTO EL FONDO /////////////////////////////////////// 
imagefill($imagen00$cFondo); 

// AGREGO UNAS LINEAS DE RELLENO //////////////////////// 
for($c=0$c <= $lineas$c++) { 
$x1=rand(0,$ancho); 
$y1=rand(0,$alto); 
$x2=rand(0,$ancho); 
$y2=rand(0,$alto); 
imageline($imagen,$x1$y1$x2$y2$cLineas); 



// GENERO EL TEXTO ALEATORIO //////////////////////////// 
$_SESSION['tmptxt'] = randomText($chars); 

// AGREGO EL TEXTO ALEATORIO A LA IMAGEN //////////////// 
imagestring($imagen5257$_SESSION['tmptxt'], $cTexto); 

// DEVUELVO LA IMAGEN GENERADA ////////////////////////// 
header("Content-type: image/jpeg"true); 
imagejpeg($imagen); 

// DESTRUYO EL OBJETO IMAGEN PARA LIBERAR MEMORIA /////// 
imagedestroy($imagen); 

?>
En el formulario coloco la siguiente línea para presentar la imagen captcha:

Código HTML:
<br>
<img src="http://www.aytoquintanar.org/images/captcha.php" width="100" height="30"> <input name="tmptxt" type="text"> 
Código de seguridad (mayúsculas, minúsculas y números)<br>
<br> 
y el código php de verificación del captcha y formulario quedaría así: (nombre archivo: enviar:php)

Código PHP:
<?php

session_start
();

// Configuracion 
$conf['mailDestinatario'] = '[email protected]'
$conf['mailAsunto'] = 'Buzon'
$conf['url_error'] = 'http://www.dominio.es/---cambiar por la página de error---'
$conf['url_ok'] = 'http://www.dominio.es/---cambiar por la página de ok---';

######################################################################
# codigo de verificacion
######################################################################

// Validar argumentos y captcha
if(!$_POST) {
  
header('Location: '.$conf['url_error']);
  exit;
}

if (
$_SESSION['tmptxt'] != $_POST['tmptxt']) { 
  
header('Location: '.$conf['url_error']); 
  exit; 
}

// Limpiar input de usuario
foreach($_POST as $id=>$value) {
  
$var[$id] = strip_tags(trim($value));
}

// Definir cuerpo del email
foreach($var as $id=>$value) {
  
$mailCuerpo .= "$id : $value\r\n";
}

// Enviar correo
if(mail($conf['mailDestinatario'], $conf['mailAsunto'], $mailCuerpo)) {
  
header('Location: '.$conf['url_ok']);
} else {
  
header('Location: '.$conf['url_error']);
}

?>
Quiero agradecer vuestra ayuda e interés, y espero que estos códigos que os dejo sirvan a alquien que los necesite.

Saludos.