I am trying to create a captcha image and store the value in session variable.
Captcha.php
<?php
session_start();
function rand_str() {
$range = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$i0=rand(0,35);
$i1=rand(0,35);
$i2=rand(0,35);
$i3=rand(0,35);
return $range[$i0].$range[$i1].$range[$i2].$range[$i3];
}
$str_captcha = rand_str();
$_SESSION['captcha'] = $str_captcha;
$im = imagecreatetruecolor(150, 70);
$color = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
imagettftext($im, 20, 0, 30, 30, $color, 'fonts/Ewert-Regular.ttf', $str_captcha);
header('Content-type: image/png');
header('Pragma: no-cache');
header('Cache-Control: no-store, no-cache, proxy-revalidate');
imagepng($im);
imagedestroy($im);
?>
Check.php
<?php
session_start();
echo $_SESSION['captcha'].'<br>';
?>
My Test Cases
Scenario 1 : For Example, if Check.php gives DMJF (generated actual value), then Captcha.php gives different image, for instance, 77B0.
If i use
$str_captcha = "ABCD";
instead of
$str_captcha = rand_str();
it works fine. Captcha.php gives ABCD image and Check.php gives ABCD string.
Scenario 2 : If i use the following
$i0=11;
$i1=12;
$i2=13;
$i3=14;
instead of
$i0=rand(0,35);
$i1=rand(0,35);
$i2=rand(0,35);
$i3=rand(0,35);
it also works fine. Captcha.php gives BCDF image and Check.php gives BCDF string.
Scenario 3 : If i use the following
$i0=11;
$i1=12;
$i2=rand(0,35);
$i3=rand(0,35);
it also works fine for last two characters. but the first two characters are different. For Example, if Check.php gives BCJF (generated actual value), then Captcha.php gives partially correct image, for instance, BC0M.
Please help me that why does the function imagettftext() takes a different value if I use rand() function.