-2

I am trying to validate captcha through ajax but its only giving me invalid captcha message ?

here is my ajax

<script>
   $(document).ready(function(){

      $('#submitcap').click(function(){

         $.post("report.php", $("#formcap").serialize(),  function(response) {
            $('#error').html(response);
         });
         return false;

      });
   });
</script>

here is my html

<span id="error"></span>
<form method="GET" id="formcap">
   <img src="<?php echo $baseUrl; ?>/captcha/captcha.php" id="captcha" /><br/>

   <!-- CHANGE TEXT LINK -->
   <a href="#" onclick="
    document.getElementById('captcha').src='<?php echo $baseUrl; ?>/captcha/captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>

   <input type="text" name="captcha" id="captcha-form" style="width:100px" autocomplete="off" /><br/>
   <input type="submit" id="submitcap"/>
</form>

Here is my PHP file

<?php
   /** Validate captcha */
   if (!empty($_REQUEST['captcha'])) {
      if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) !=$_SESSION['captcha']) {
         $captcha_message = "Invalid captcha";
         $style = "background-color: #FF606C";
      } else {
         $captcha_message = "Valid captcha";
         $style = "background-color: #CCFF99";
      }

      $request_captcha = htmlspecialchars($_REQUEST['captcha']);

      echo <<<HTML
      <div id="result" style="$style">
          <h2>$captcha_message</h2>
          <table>
          <tr>
             <td>Session CAPTCHA:</td>
             <td>{$_SESSION['captcha']}</td>
          </tr>
          <tr>
             <td>Form CAPTCHA:</td>
             <td>$request_captcha</td>
          </tr>
       </table>
     </div>
HTML;
     unset($_SESSION['captcha']);
   }
?>

I dont know why i am getting Invalid captcha message If i validate this captcha without ajax it works fine

4

1 回答 1

0

我认为其他人的意思是您在应该使用 $_POST 时使用了 $_REQUEST,因为您通过使用 jQuery .post 函数将数据发布到此表单。

您需要做的是调试接收已发布数据的 PHP 代码。我承认这对初学者来说似乎有点令人生畏,但这里有一个建议。因为您无法将该代码中的数据回显到屏幕上,请尝试将其写入文件以供您在每次执行后进行检查。

<?php
  /** Validate captcha */

  // show whats actually in the session file as this point
  file_put_contents( 'captcha.log', 'SESSION ' . print_r($_SESSION, TRUE) );   

  // show what you passed to the script
  file_put_contents( 'captcha.log', 'POST ' . print_r($_POST, TRUE), FILE_APPEND );  

  /*
   * Now you can edit the captcha.log file which will be in the same directory as this script
   */


  if (!empty($_POST['captcha'])) {
     if (empty($_SESSION['captcha']) || trim(strtolower($_POST['captcha'])) != $_SESSION['captcha']) {
        $captcha_message = "Invalid captcha";
        $style = "background-color: #FF606C";
     } else {
        $captcha_message = "Valid captcha";
        $style = "background-color: #CCFF99";
     }

....

这应该允许您在运行 !=测试之前检查 $_POST['captcha'] 和 $_SESSION['captcha'] 这两个值是什么。

作为参考,$_REQUEST 数组是 $_POST 和 $_GET 数组的合并,因此虽然在某些情况下使用它并没有完全错误,但它确实会使您的代码被滥用,因为尝试破解它要容易得多只需使用类似的查询字符串调用它www.site.tld/script_name.php?captcha=aaa。这将创建一个带有 $_GET['captcha'] = 'aaa' 的 $_GET 数组,因此创建一个带有 $_REQUEST['captcha'] = 'aaa' 的 $_REQUEST 数组。

如果您的代码期望通过 POST 提供数据,例如您的,因此加载 $_POST 数组使用 $_POST 更安全,那么您将永远不会被使用查询字符串的黑客所困扰,因为您永远不会看到它和您的参数验证将导致您的脚本采用“未提供数据”退出路线。

虽然伪造发布的数据有点困难,但并不多。

于 2013-08-15T08:47:06.457 回答