Foros del Web » Programando para Internet » PHP »

Problema con CAPTCHA

Estas en el tema de Problema con CAPTCHA en el foro de PHP en Foros del Web. Hola amigos. Tengo un script para crear imagenes CAPTCHA que vi en un manual hace algún tiempo, todo funcionaba bien hasta que hace un par ...
  #1 (permalink)  
Antiguo 30/01/2009, 23:41
Avatar de ZydRick  
Fecha de Ingreso: febrero-2005
Ubicación: Lima
Mensajes: 750
Antigüedad: 19 años, 2 meses
Puntos: 4
Problema con CAPTCHA

Hola amigos.

Tengo un script para crear imagenes CAPTCHA que vi en un manual hace algún tiempo, todo funcionaba bien hasta que hace un par de días mudé mi página de un hosting con PHP 5.2.6 a otro con PHP 5.2.8 y el script dejó de funcionar, y no consigo dar con la falla, el script es el siguiente:

Código PHP:
<?php
    
// Start the session so we can store our generated key inside it for later retrieval
    
session_start();
    
// Set to whatever size you want, or randomize for more security
    
$captchaTextSize 7;
    do {
        
// Generate a random string and encrypt it with md5
        
$md5Hash md5microtime( ) * mktime( ) );
        
// Remove any hard to distinguish characters from our hash
        
preg_replace('([1aeilou0])'""$md5Hash );
    }while(
strlen$md5Hash ) < $captchaTextSize );
    
// we need only 7 characters for this captcha
    
$key substr$md5Hash0$captchaTextSize );
    
// Add the newly generated key to the session. Note, it is encrypted.
    
$_SESSION['key'] = md5($key);
    
// grab the base image from our pre-generated captcha image background
    
$captchaImage imagecreatefrompng"images/captcha.png" );
    
/* 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. */ 
    
$textColor imagecolorallocate$captchaImage3111892 );
    
/* 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 */
    
$lineColor imagecolorallocate$captchaImage15103103 );
    
// get the size parameters of our image
    
$imageInfo getimagesize("images/captcha.png");
    
// decide how many lines you want to draw
    
$linesToDraw 10;
    
// Add the lines randomly to the image
    
for( $i 0$i $linesToDraw$i++ )  {
        
// generate random start spots and end spots
        
$xStart mt_rand0$imageInfo] );
        
$xEnd mt_rand0$imageInfo] );
        
// Draw the line to the captcha
        
imageline$captchaImage$xStart0$xEnd$imageInfo[1], $lineColor );
    }
    
/* 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. */
    
imagettftext$captchaImage2003535$textColor"fonts/VeraBd.ttf"$key );
    
// Output the image to the browser, header settings prevent caching
    
header("Content-type: image/png");
    
header("Cache-Control: no-cache, must-revalidate");
    
header("Expires: Fri, 19 Jan 1994 05:00:00 GMT");
    
header("Pragma: no-cache");
    
imagepng$captchaImage );
?>
El formulario donde uso el captcha y valido si el código fue ingresado correctamente o no es este:

Código PHP:
<?php
    session_start
();
    if(isset(
$_POST['enviar'])) {
        function 
email_valido($correo) {
            if (
eregi("^[_\.0-9a-z-]+@[0-9a-z\._\-]+\.[a-z]{2,4}$"$correo)) return true;
            else return 
false;
        }
        if(
$_POST['nombre'] == '') {
            echo 
"<p style='color: #ff0000;'><strong>No has ingresado tu Nombre.</strong></p>";
        }elseif(
$_POST['email'] == '') {
            echo 
"<p style='color: #ff0000;'><strong>No has ingresado tu Email.</strong></p>";
        }elseif(!
email_valido($_POST['email'])) {
            echo 
"<p style='color: #ff0000;'><strong>El Email ".$_POST['email']." no es una dirección válida.</strong></p>";
        }elseif(
$_POST['asunto'] == '') {
            echo 
"<p style='color: #ff0000;'><strong>No has ingresado el Asunto del mensaje.</strong></p>";
        }elseif(
$_POST['mensaje'] == '') {
            echo 
"<p style='color: #ff0000;'><strong>No has ingresado el Mensaje.</strong></p>";
        }elseif(
md5($_POST['code']) != $_SESSION['key']) {
            echo 
"<p style='color: #ff0000;'><strong>El c&oacute;digo de validaci&oacute;n no ha sido ingresado o es incorrecto.</strong></p>";
        }else {
            
$nombre $_POST['nombre'];
            
$email $_POST['email'];
            
$asunto $_POST['asunto'];
            
$para "[email protected]";
            
$mensaje '<div style="background: #f7f7f7; border: 1px solid #e0e0e0; padding: 10px;">
                            <p style="margin: 10px 0;"><strong>Mensaje enviado desde CelulaWeb.NET</strong></p>
                            '
.nl2br($_POST[mensaje]).'
                        </div>'
;
            
$sheader "From:".$nombre." <".$email.">\nReply-To:".$email."\n";
            
$sheader $sheader."Mime-Version: 1.0\n";
            
$sheader $sheader."Content-Type: text/html";
            
mail($para,$asunto,$mensaje,$sheader);
            echo 
"<div style='color: #63a915;'><strong>El mensaje se ha enviado correctamente. Gracias.</strong></div>";
        }
    }
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
    <label class="reg">Nombre completo:</label>
    <input type="text" name="nombre" id="nombre" style="width: 95%;" />
    <label class="reg">Correo electr&oacute;nico:</label>
    <input name="email" type="text" id="email" style="width: 95%;" />
    <label class="reg">Asunto:</label>
    <input name="asunto" type="text" id="asunto" style="width: 95%;" />
    <label class="reg">Mensaje:</label>
    <textarea name="mensaje" cols="75" rows="10" id="mensaje"></textarea>
    <label class="reg">Código de seguridad:</label>
    <img src="captcha.php" style="float: left; margin-right: 5px;" /> <input type="text" name="code" maxlength="7" size="7" style="font-size: 1.8em; font-weight: bold; color: #1F765C;" />
    <p style="padding-top: 5px;">
    <input type="submit" name="enviar" value="Enviar Mensaje" />
    <input type="reset" name="enviar" value="Borrar datos" />
    </p>
</form>
Al ejecutar el la imagen no muestra 7 caracteres como lo indica la variable $captchaTextSize, sino que muestra más y es ahí donde no sé qué pueda estar fallando, por un momento pensé que se trataría de un problema en las sesiones, quité el sessión_start(); del archivo que genera la imagen y al ejecutarlo muestra los 7 caracteres pero al enviar el formulario con el código correctamente escrito me dice que no es correcto.

A ver si alguno de ustedes me pueda echar una mano con esto, gracias de antemano.

Saludos.
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 04:28.