Foros del Web » Programando para Internet » PHP »

Captcha

Estas en el tema de Captcha en el foro de PHP en Foros del Web. Hola a todos, ya tengo armado un formulario con todos los campos y busque en internet y encontre un codigo captcha, lo que no se ...
  #1 (permalink)  
Antiguo 03/04/2014, 19:03
Avatar de fedefrankk  
Fecha de Ingreso: agosto-2007
Mensajes: 871
Antigüedad: 16 años, 7 meses
Puntos: 7
Pregunta Captcha

Hola a todos, ya tengo armado un formulario con todos los campos y busque en internet y encontre un codigo captcha, lo que no se hacer es que no tenga tantos caracteres, quiero uno bajarlo a 5 caracteres pero no se como hacerlo...

dejo codigo

Código PHP:
Ver original
  1. <?php
  2.  
  3. // Start the session so we can store our generated key inside it for later retrieval
  4.  
  5.  
  6. // Set to whatever size you want, or randomize for more security
  7.  
  8. $captchaTextSize = 7;
  9.  
  10. do {
  11.  
  12.     // Generate a random string and encrypt it with md5
  13.  
  14.     $md5Hash = md5( microtime( ) * mktime( ) );
  15.  
  16.     // Remove any hard to distinguish characters from our hash
  17.  
  18.     preg_replace( '([1aeilou0])', "", $md5Hash );
  19.  
  20. } while( strlen( $md5Hash ) < $captchaTextSize );
  21.  
  22. // we need only 7 characters for this captcha
  23.  
  24. $key = substr( $md5Hash, 0, $captchaTextSize );
  25.  
  26. // Add the newly generated key to the session. Note, it is encrypted.
  27.  
  28. $_SESSION['key'] = md5( $key );
  29.  
  30. // grab the base image from our pre-generated captcha image background
  31.  
  32. $captchaImage = imagecreatefrompng( "images/captcha.png" );
  33.  
  34. /*
  35.  
  36. Select a color for the text. Since our background is an aqua/greenish color, we choose a text color that will stand out, but not completely. A slightly darker green in our case.
  37.  
  38. */
  39.  
  40. $textColor = imagecolorallocate( $captchaImage, 31, 118, 92 );
  41.  
  42. /*
  43.  
  44. Select a color for the random lines we want to draw on top of the image, in this case, we are going to use another shade of green/blue
  45.  
  46. */
  47.  
  48. $lineColor = imagecolorallocate( $captchaImage, 15, 103, 103 );
  49.  
  50. // get the size parameters of our image
  51.  
  52. $imageInfo = getimagesize( "images/captcha.png" );
  53.  
  54. // decide how many lines you want to draw
  55.  
  56. $linesToDraw = 10;
  57.  
  58. // Add the lines randomly to the image
  59.  
  60. for( $i = 0; $i < $linesToDraw; $i++ )  {
  61.  
  62.     // generate random start spots and end spots
  63.  
  64.     $xStart = mt_rand( 0, $imageInfo[ 0 ] );
  65.     $xEnd = mt_rand( 0, $imageInfo[ 0 ] );
  66.  
  67.     // Draw the line to the captcha
  68.  
  69.     imageline( $captchaImage, $xStart, 0, $xEnd, $imageInfo[1], $lineColor );
  70.  
  71. }
  72.  
  73. /*
  74.  
  75. Draw our randomly generated string to our captcha using the given true type font. In this case, I am using BitStream Vera Sans Bold, but you could modify it to any other font you wanted to use.
  76.  
  77. */
  78.  
  79. imagettftext( $captchaImage, 20, 0, 35, 35, $textColor, "fonts/VeraBd.ttf", $key );
  80.  
  81. // Output the image to the browser, header settings prevent caching
  82.  
  83. header ( "Content-type: image/png" );
  84.  
  85. header("Cache-Control: no-cache, must-revalidate");
  86. header("Expires: Fri, 19 Jan 1994 05:00:00 GMT");
  87. header("Pragma: no-cache");
  88.  
  89. imagepng( $captchaImage );
  90.  
  91. ?>

Saludos a todos y gracias de antemano..
saludos
fede
  #2 (permalink)  
Antiguo 03/04/2014, 19:36
Avatar de oscard41  
Fecha de Ingreso: mayo-2012
Ubicación: Caracas
Mensajes: 288
Antigüedad: 11 años, 10 meses
Puntos: 8
Respuesta: Captcha

creo que debes cambiar el valor de la variable $linesToDraw a 5

$linesToDraw = 5;


Saludos
  #3 (permalink)  
Antiguo 04/04/2014, 00:04
 
Fecha de Ingreso: octubre-2012
Mensajes: 135
Antigüedad: 11 años, 5 meses
Puntos: 8
Es la variable captchaTextSize, la primera que se define. Si lees el comentario(inglés) verás el funcionamiento
  #4 (permalink)  
Antiguo 04/04/2014, 10:35
Avatar de fedefrankk  
Fecha de Ingreso: agosto-2007
Mensajes: 871
Antigüedad: 16 años, 7 meses
Puntos: 7
Pregunta Respuesta: Captcha

Buenas, lo hice y no funciono, apague el pc, lo prendi y si funciono, ahora, si yo pongo 5, me salen 5 caracteres, si apreto actualizar me salen 10 caracteres, o mas,

Osea la primera vez salen 5 caracter y siactualizo me salen mas de 10 o 10.

Código PHP:
Ver original
  1. $captchaTextSize = 5;

Este archivo si todo esta bien pasa a otro,

Código PHP:
Ver original
  1. <?php
  2.  
  3. session_start( ); // allows us to retrieve our key form the session
  4.  
  5. /*
  6.  
  7. First encrypt the key passed by the form, then compare it to the already encrypted key we have stored inside our session variable
  8.  
  9. */
  10.  
  11. if( md5( $_POST[ 'code' ] ) != $_SESSION[ 'key' ] ) {
  12.  
  13.        echo "You ented the wrong code, please try again!";
  14.  
  15. } else {
  16.  
  17.        echo "Success, you ented the correct code, rock and roll...";
  18.  
  19. }
  20.  
  21. ?>


en este ultimo pude ser que tenga que eliminar la session?... creo que no tiene nada que ver....

Saludos y gracias por sus respuestas.!!!

fede

Última edición por fedefrankk; 04/04/2014 a las 10:36 Razón: error

Etiquetas: captcha, formulario, select
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:25.