Foros del Web » Programando para Internet » PHP »

[APORTE] Captcha / Verificacion De la Imagen

Estas en el tema de [APORTE] Captcha / Verificacion De la Imagen en el foro de PHP en Foros del Web. HOla amigos bueno estube trabajando en en una captcha algo muy sencillo y logre esto y quiero conpartirlo con ustedes. Requisitos: Imagen: Fuente Deseada: (en ...
  #1 (permalink)  
Antiguo 21/09/2009, 16:01
Avatar de chulifo  
Fecha de Ingreso: abril-2009
Ubicación: perdido en codigos del PHP, pero aprendo rapido!
Mensajes: 524
Antigüedad: 15 años
Puntos: 18
[APORTE] Captcha / Verificacion De la Imagen

HOla amigos bueno estube trabajando en en una captcha algo muy sencillo y logre esto y quiero conpartirlo con ustedes.

Requisitos:

Imagen:
Fuente Deseada: (en este caso se usa una fuente Arial TrueType)
captcha.php

Código php:
Ver original
  1. <?php
  2. $_SESSION['captcha']=rand(0001,9999);
  3. if(isset($_GET['img'])){
  4. $button_text =$_SESSION['captcha'];
  5. if (empty($button_text))
  6. {
  7.     echo "ERROR";
  8.     exit;
  9. }
  10. $im = imagecreatefrompng ("capcha.png");
  11. $width_image = imagesx($im);
  12. $height_image = imagesy($im);
  13. $width_image_wo_margin = $width_image - (2 * 8);
  14. $height_image_wo_margin = $height_image - (2 * 8);
  15. $font_size = 30;
  16. do
  17. {
  18.     $font_size--;  
  19.     $bbox=imagettfbbox ($font_size, 0, "arial.ttf", $button_text);
  20.     $right_text = $bbox[2];
  21.     $left_text = $bbox[0];
  22.     $width_text = $right_text - $left_text;
  23.     $height_text = abs($bbox[7] - $bbox[1]);
  24.    
  25. } while ( $font_size>8 &&
  26.          ( $height_text>$height_image_wo_margin ||
  27.           $width_text>$width_image_wo_margin )
  28.          );
  29. if ( $height_text>$height_image_wo_margin ||
  30.     $width_text>$width_image_wo_margin )
  31. {
  32.     echo "Disculpe Su Texto Es Muy Grande.<br>";
  33. }
  34. else
  35. {
  36.     $text_x = $width_image/2.0 - $width_text/2.0;
  37.     $text_y = $height_image/2.0 - $height_text/2.0 ;
  38.     if ($left_text < 0)
  39.     $text_x += abs($left_text);
  40. $above_line_text = abs($bbox[7]);
  41.     $text_y += $above_line_text;
  42.     $text_y -= 2;
  43.     $white = imagecolorallocate ($im, 255, 255, 255);
  44.     imagettftext ($im, $font_size, 0, $text_x, $text_y, $white, "arial.ttf",
  45.                   $button_text);   
  46.     header ("Content-type: image/png");
  47.     imagepng ($im);
  48. }
  49. }
  50. ?>
  51. <body>
  52. <form id="form1" name="form1" method="post" action="verifica.php">
  53.   <p>
  54.     <input type="text" name="verificar" id="verificar" />
  55.   <img src="?img" width="100" height="30" /></p>
  56.   <p>
  57.     <input type="submit" name="button" id="button" value="Enviar" /></p>
  58. </form>
  59.  
  60. </body>

verifica.php
Código php:
Ver original
  1. <?php
  2. if ($_POST['verificar'] == $_SESSION['captcha']){
  3.     echo 'Paso La Prueba De Verificación';
  4. }
  5.     else
  6.     { echo 'La Letras Ingresadas No coinsiden con la imagen';
  7.     }
  8. ?>

si tiene sugerencias =D
__________________
Solo soy un simple fanático que mata el tiempo de la mejor manera.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:22.