数学验证码会话值未通过

时间:2013-04-23 00:55:49

标签: php javascript validation session captcha

我正在处理联系表格。我的问题是我无法验证验证码。 isset($_session[''])不起作用。

我的代码:

contact.php

<?php
  session_start();
  if (!isset($_SESSION['num1']) && !isset($_SESSION['num2'])) {
    $_SESSION['num1'] = rand(1,5);
    $_SESSION['num2'] = rand(1,5);
  }
?>

显示验证码字段:

<label for="captcha">
  <?php echo $_SESSION['num1']; ?>+ <?php echo $_SESSION['num2']; ?>?
</label>
<input type="text" id="captcha" name="captcha" placeholder="Captcha">

JavaScript的:

$(document).ready(function() {
  $.validator.addMethod("nourl", function(value, element) {
    return !/http\:\/\/|www\.|link\=|url\=/.test(value);
  }, "No URL's");

  $("form").validate({
    rules: {
      name: {
        required: true
      },
      email: {
        required: true,
        email: true
      },
      comments: {
        required: true,
        minlength: 4,
        nourl: true
      },
      captcha: {
        required: true,
        remote: {
          url: "validation.php",
          type: "post"
        }
      }
    },
    messages: {
      name: "",
      email: "",
      comments: "",
      captcha: ""
    }
  });
});

validation.php:

<?php
  session_start();
  if (!isset($_SESSION['num1']) || !isset($_SESSION['num2'])) {
    exit('"Unknown session"');
  }
  $sum = (int)$_SESSION['num1'] + (int)$_SESSION['num2'];
  if (isset($_POST['captcha']) && (int)$_POST['captcha'] === $sum) {
    exit('true');
  }
  exit('false');
?>

我得到的结果:“未知会话”

每次都让这一行返回。

if (!isset($_SESSION['num1']) || !isset($_SESSION['num2'])) {
  exit('"Unknown session"');
}

有人可以帮忙吗?

0 个答案:

没有答案