Ajax php GET方法不起作用?

时间:2013-08-15 08:01:17

标签: php javascript jquery ajax

我正在尝试通过ajax验证验证码,但它只给我无效的验证码消息?

这是我的ajax

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

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

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

      });
   });
</script>

这是我的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>

这是我的PHP文件

<?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']);
   }
?>

我不知道为什么我得到无效的验证码消息如果我在没有ajax的情况下验证这个验证码它可以正常工作

1 个答案:

答案 0 :(得分: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']的2个值。

作为参考,$ _REQUEST数组是$ _POST和$ _GET数组的合并,所以虽然在某些情况下使用它并不完全错误,但它确实会打开你的代码而被滥用,因为它更容易尝试和通过使用像www.site.tld/script_name.php?captcha=aaa之类的查询字符串来调用它来破解它。这将使用$ _GET ['captcha'] ='aaa'创建$ _GET数组,因此使用$ _REQUEST ['captcha'] ='aaa'创建$ _REQUEST数组。

如果你的代码期望通过POST提供数据,就像你的那样,并因此加载$ _POST数组使用$ _POST更安全,那么你将永远不会被使用查询字符串的hack打扰,因为你永远不会看到它和您的参数验证将使您的脚本采用“无数据提供”退出路径。

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