0

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

我的代码:

联系人.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: ""
    }
  });
});

验证.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"');
}

有人可以帮忙吗?

4

0 回答 0