Cuando genero mi imagen captcha, guardo el código a escribir en la imagen en una sesión, para que al enviar el formulario pueda comparar lo que escribió el usuario con la sesión y si son iguales enviar el formulario, pero cuando comparo estos dos, nunca concuerdan, me he dado cuenta que el código que se escribe en la imagen no es el mismo que se guarda en la sesión, a pesar de guardar y escribir el código al mismo tiempo, y cada vez que actualizo el formulario lo que queda guardado en la sesión es el código que se escribió en la imagen anterior.
Les muestro el código que uso:
captcha.php
Código PHP:
<?php
session_start();
$width = 35;
$height = 150;
function getCode($length){
$code = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$codigo = '';
$i = 0;
while ($i < $length) {
$codigo .= substr($code, mt_rand(0, strlen($code)-1), 1);
$i++;
}
return $codigo;
}
$_SESSION['captcha'] = getCode(6);
$image = imagecreatetruecolor($height, $width);
$width = imagesx($image);
$height = imagesy($image);
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$red = imagecolorallocatealpha($image, 255, 0, 0, 75);
$green = imagecolorallocatealpha($image, 0, 255, 0, 75);
$blue = imagecolorallocatealpha($image, 0, 0, 255, 75);
imagefilledrectangle($image, 0, 0, $width, $height, $white);
imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $red);
imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $green);
imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $blue);
imagefilledrectangle($image, 0, 0, $width, 0, $black);
imagefilledrectangle($image, $width - 1, 0, $width - 1, $height - 1, $black);
imagefilledrectangle($image, 0, 0, 0, $height - 1, $black);
imagefilledrectangle($image, 0, $height - 1, $width, $height - 1, $black);
imagestring($image, 10, intval(($width - (strlen($_SESSION['captcha']) * 9)) / 2), intval(($height - 15) / 2), $_SESSION['captcha'], $black);
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
Código PHP:
<b>Introduzca el código en el cuadro de abajo:</b><br />
<input type="text" name="captcha" value="" />
<br />
<img src="captcha.php" id="captcha" />
<? echo $_SESSION['captcha'];?>
muchas gracias por sus respuestas, saludos