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 = md5( microtime( ) * 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( $md5Hash, 0, $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( $captchaImage, 31, 118, 92 );
/* 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( $captchaImage, 15, 103, 103 );
// 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_rand( 0, $imageInfo[ 0 ] );
$xEnd = mt_rand( 0, $imageInfo[ 0 ] );
// Draw the line to the captcha
imageline( $captchaImage, $xStart, 0, $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( $captchaImage, 20, 0, 35, 35, $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 );
?>
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ódigo de validació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ó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>
A ver si alguno de ustedes me pueda echar una mano con esto, gracias de antemano.
Saludos.