Всем привет. Помогите пожалуйста разобраться. Проблема такая. Вставляю на сайт капчу. Вот скрипт, который генерирует картинку <?php session_start(); class CaptchaSecurityImages { var $font = 'monofont.ttf'; function generateCode($characters) { /* list all possible characters, similar looking characters and vowels have been removed */ $possible = '23456789bcdfghjkmnpqrstvwxyz'; $code = ''; $i = 0; while ($i < $characters) { $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } return $code; } function CaptchaSecurityImages($width='120',$height='40',$characters='6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for( $i=0; $i<($width*$height)/3; $i++ ) { imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); } /* generate random lines in background */ for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4])/2; $y = ($height - $textbox[5])/2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; } } $width = isset($_GET['width']) ? $_GET['width'] : '120'; $height = isset($_GET['height']) ? $_GET['height'] : '40'; $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6'; $captcha = new CaptchaSecurityImages($width,$height,$characters); ?> Вот то, где она размещается на сайте <form action="comment.php" method="post" name="form_com"> <p class='post_text'><label>Ваше имя: </label><input name="author" type="text" size="41" maxlength="30"></p> <p class='post_text'><label>Текст сообщения: <br><textarea name="text" cols="41" rows="4"></textarea></label></p> <p class='post_text'><img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /></p> <p class='post_text'><label for="security_code">Введите буквы с картинки: </label><input id="security_code" name="security_code" type="text" /></p> <p class='post_text'><input name="sub_com" type="submit" value="Добавить"></p> </form> А вот скрипт сравнения: <?php include ("db.php"); if (isset($_POST['author'])) { $author = $_POST['author']; } if (isset($_POST['text'])) { $text = $_POST['text']; } if (isset($_POST['security_code'])) { $security_code = $_POST['security_code']; } if (isset($_POST['sub_com'])) { $sub_com = $_POST['sub_com']; } if (isset($sub_com)) { if (isset($author)) {trim ($author);} else {$author="";} if (isset($text)) {trim ($text);} else {$text="";} if (empty($author) or empty($text)) {exit ("<p class='post_text'>Заполнены не все поля. Вернитесь назад.<br> <input name='back' type='button' value='Назад' onclick = 'javascript:self.back();'></p>");} $author=stripslashes($author); $text=stripslashes($text); $author=stripslashes($author); $text=stripslashes($text); if ($security_code == $_SESSION['security_code']) { $date = date ("Y-m-d"); $result = mysql_query ("INSERT INTO comments (author, text, date) VALUES ('$author', '$text', '$date')",$db); } else {exit ("<p class='post_text'>Вы ввели неверный код. Вернитесь назад.<br> <input name='back' type='button' value='Назад' onclick = 'javascript:self.back();'></p>");} } ?> Проблема в том,что при корректном вводе кода с картинки, все равно выводится сообщение о том что он введен неверно и сообщение соответственно не добавляется. Помогите мне, пожалуйста.
1. При генерации каптчи, код записывается в переменную сессии $_SESSION['security_code']. Для того, чтобы эта переменная была доступна в другом скрипте, необходимо в самом начале этого скрипта (ДО какого-либо вывода в браузер) вызвать функцию session_start() , которая, скажем так, активирует переменные сессии. Посмотри, как вызывается эта функция в скрипте, который генерирует каптчу. 2. <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /> Передавать количество символов, которые нужно отрисовать через GET - это бред. Робот сможет подставлять туда самое маленькое возможное значение (вместо пяти) и в разы чаще разгадывать каптчу. &characters=5 - можно просто убрать, а в скрипте, который генерирует каптчу, строку PHP: <?php // ... $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6'; заменить на: PHP: <?php // ... $characters = mt_rand(5, 7); 3. Используй подсветку кода в следующий раз.