Каптча вводится со второго раза

Здравствуйте. Поставил плагин trymath. При первой попытке отправить комментарий и вводе ответа на секретный вопрос, всегда выдается ошибка Invalid Code, при второй и дальнейших комментарий отправляется успешно. Через сутки опять та же история – при первой попытке комментарий не отправляется, при дальнейших все нормально. Предполагаю что проблема с куками. Помогите найти ошибку. Код прилагаю.

<?php
require_once ('phpfiglet_class.php');

$newTrymath = new trymath;

class trymath {
  var $num1;
  var $num2;
  var $op;
  var $formula;
  var $figlet;
  var $fonts = array ('doom', 'fuzzy', 'standard', 'avatar', 'alphabet','bell', 'big', 'banner3');
  var $font;
  var $salt = 'ushcompu.com.ar';

  function trymath () {
        add_action('comment_form', array("trymath", "draw_form"), 9999);
        add_action('comment_post', array("trymath", "comment_post"));
  }

    function draw_form ($id) {
    global $newTrymath, $user_ID;
    if ($user_ID) return $id;
    @session_start ();
    $newTrymath -> generate ();
    $_SESSION['trymath'] = md5 ($newTrymath -> salt . $newTrymath -> getResult ());
?>
<div id="trymath_cont">
 <p>
  <label for="trymath">
    Seguridad:
  </label>
  <input type="text" name="trymath" id="trymath" />
  <br /> =
 </p>
 <pre><?php echo $newTrymath -> fetch () ?></pre>
</div>
<script type="text/javascript">
//<![CDATA[
//for( i = 0; i < document.forms.length; i++ ) {
//    if( typeof(document.forms[i].trymath) != 'undefined' ) {
//        commentForm = document.forms[i].comment.parentNode;
//        break;
//    }
//}
//var commentArea = commentForm.parentNode;
//var captchafrm = document.getElementById("trymath_cont");
//commentArea.insertBefore(captchafrm, commentForm);
//commentArea.trymath.size = commentArea.author.size;
//commentArea.trymath.className = commentArea.author.className;
function insertAfter (referenceNode, newNode) {
  referenceNode.parentNode.insertBefore (newNode, referenceNode.nextSibling);
}
var captchafrm = document.getElementById("trymath_cont");
var url = document.getElementById ('url');
if (url != undefined) {
  insertAfter (url, captchafrm);
}
//]]>
</script>
<?php
  }

  function comment_post ($id) {
    global $newTrymath, $user_ID;
    if ($user_ID) return $id;
    session_start ();
        $publicTrymath = $_POST['trymath'];
    if (md5 ($newTrymath -> salt . $publicTrymath) == $_SESSION['trymath'])
      return $id;
        wp_set_comment_status ($id, 'delete');

        ?><html>
            <head><title>Invalid Code</title></head>
            <body>
        Invalid Code
            </body>
        </html>
        <?php
        exit();
  }

  function generate () {
    global $newTrymath;
    $newTrymath -> figlet = new phpFiglet ();
    $newTrymath -> font = $newTrymath -> fonts[rand(0, count ($newTrymath -> fonts) - 1)];
    $newTrymath -> figlet -> loadFont (dirname (__FILE__) . '/fonts/' . $newTrymath -> font . '.flf');

    $op = mt_rand (0, 3);
    switch ($op) {
      case 0:
        $sign = '+';
        $num1 = mt_rand (0, 99);
        $num2 = mt_rand (0, 99);
        $result = $num1 + $num2;
        break;
      case 1:
        $sign = '-';
        $num1 = mt_rand (0, 99);
        $num2 = mt_rand (0, 99);
        if ($num2 > $num1) {
          $aux = $num2;
          $num2 = $num1;
          $num1 = $aux;
        }
        $result = $num1 - $num2;
        break;
      case 2:
        $sign = '*';
        $num1 = mt_rand (0, 9);
        $num2 = mt_rand (0, 9);
        $result = $num1 * $num2;
        break;
      case 3:
        $sign = '/';
        $num2 = mt_rand (1, 9);
        $result = mt_rand (1, 9);
        $num1 = $num2 * $result;
        break;
    }
    $newTrymath -> op = $op;
    $newTrymath -> num1 = $num1;
    $newTrymath -> num2 = $num2;
    $newTrymath -> result = $result;
    $newTrymath -> formula = $num1 . $sign . $num2;
  }
  function getResult  () { global $newTrymath; return $newTrymath -> result; }
  function getFormula () { global $newTrymath; return $newTrymath -> formula; }
  function fetch () {
    global $newTrymath;
    return $newTrymath -> figlet -> fetch ($newTrymath -> formula);
  }
}
?>

Добавлю, появляется такая ошибка если убрать @ перед первым вызовом session_start ();.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/serv/data/www/serv/wp-content/themes/theme/single.php:1) in /var/www/serv/data/www/serv/wp-content/plugins/trymath/trymath.php on line 39

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/serv/data/www/serv/wp-content/themes/theme/single.php:1) in /var/www/serv/data/www/serv/wp-content/plugins/trymath/trymath.php on line 39

Подскажите в чем может быть проблема?

Добавлю, появляется такая ошибка если убрать @ перед первым вызовом session_start ();. 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/serv/data/www/serv/wp-content/themes/theme/single.php:1) in /var/www/serv/data/www/serv/wp-content/plugins/trymath/trymath.php on line 39

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/serv/data/www/serv/wp-content/themes/theme/single.php:1) in /var/www/serv/data/www/serv/wp-content/plugins/trymath/trymath.php on line 39

Подскажите в чем может быть проблема?

Подскажу.. Вывод первого сообщения на страницу ( echo или print) происходит раньше, чем session_start) так что session_start лучше всего прописать в начале страницы до вывода первого сообщения (оно меняет заголовок http ответа) вот как раз этот скрипт и выводит первое сообщение wp-content/themes/theme/single.php

а вобще есть хорошая альтернатива этим распростарненным капчам, а известно всем, чем больше распространена капча, тем больше ботов могут ее распознать. Вот я на блоге у себя написал про нее и поставил, http://www.ilyaplot.ru/2010/03/revolyuciya-v-zashhite-bloga-ot-spam-botov/ можете посмотреть в живую как защищает хорошо.

Хотя были мысли написать свои, вместо субмита сделать imageЖ, при щелчке она передает координаты, но я не придумал еще как их безопасно проверить…. Как придумаю, напишу. Будет единственная в своем роде такого типа =)

Anonymous
Отправить
Ответ на: