I'm using the following code on a file called random.php that is called by a form to create the CAPTCHA image.
The one problem is that users don't have to fill out the form, hence they visit a page, create a session (from random.php) and from then on all the pages show up with a blank CAPTCHA image as the session var 'rand_code' isn't empty.
Is there a way I can stop this..give the session a random name when it is started or something so that a different one is started ever time random.php is run???
<?php
session_start();
if (empty($_SESSION['rand_code'])) {
$str = "";
$length = 0;
for ($i = 0; $i < 4; $i++) {
// this numbers refer to numbers of the ascii table (small-caps)
$str .= chr(rand(97, 122));
}
$_SESSION['rand_code'] = $str;
} $imgX = 60;
$imgY = 20;
$image = imagecreatetruecolor(60, 20);
$backgr_col = imagecolorallocate($image, 238,239,239);
$border_col = imagecolorallocate($image, 208,208,208);
$text_col = imagecolorallocate($image, 46,60,31);
imagefilledrectangle($image, 0, 0, 60, 20, $backgr_col);
imagerectangle($image, 0, 0, 59, 19, $border_col);
$font = "fonts/LucidaTypewriterBold.ttf";
$font_size = 10;
$angle = 0;
$box = imagettfbbox($font_size, $angle, $font, $str);
$x = (int)($imgX - $box[4]) / 2;
$y = (int)($imgY - $box[5]) / 2;
imagettftext($image, $font_size, $angle, $x, $y, $text_col, $font, $str);
header("Content-type: image/png");
imagepng($image);
imagedestroy ($image);
?>
<label for="validator">Validation Code</label>
<input type="text" name="validator" size="4" />
<img src="random.php" alt="CAPTCHA image" width="60" height="20" vspace="1" align="top" />
Thanks for the help guys.