【问题标题】:Jquery PHP Captcha ValidationJquery PHP验证码验证
【发布时间】:2015-10-07 08:34:33
【问题描述】:

我在表单中使用了验证码,如果我尝试点击提交(通过鼠标),即使在给出正确的验证码之后,我得到的警告是 “Wrong Captcha”,可悲的是问题是,即使我输入了错误的验证码,如果我按下回车键(通过键盘),它也会显示“谢谢”文本。

index.php

    <img src="get_captcha.php" alt="" id="captcha" />
    <br clear="all" />
    <input name="code" class="smoothborder" type="text" id="code" required="required" onblur="checkcode(this.value)">
    <div id="wrong" class="error" style="display: none;">Wrong Captcha</div>
    </div>
    <img src="refresh.jpg" width="25" alt="" id="refresh" style="margin-top:66px;margin-left:10px;" />

    function checkcode(value){
        $('#wrong').hide();
        $.post( "contact.php",{ namevalue: "code" }, function( data ) {     
            var returndata = data;      
        if(returndata == value){            
        } else {            
            $('#wrong').show();
            $('#code').val('');         
        }  
    });
  }

联系方式.php

    <?php
    ob_start();
    session_start();
    if(isset($_REQUEST['namevalue']) != ''){
        echo $_SESSION['random_number'];
    }else{
        $name =  $_POST['name'];
        $email =  $_POST['email'];
        $mobile =  $_POST['mobile_no'];
        $url =  $_POST['url'];
        $comment =  $_POST['comment'];
        $response =  $_POST['response'];
        $Chooseoption =  $_POST['ddlselectionvalue'];
        $to = 'thejasvi@1800xchange.com';   
        $from = $name . ' <' . $email . '>';

        $subject = 'Message from ' . $name; 
        $message = 'Select Option: ' . $Chooseoption . '<br/>
        Name: ' . $name . '<br/>
        Email: ' . $email . '<br/>
        Mobile No.: ' . $mobile . '<br/>    
        URL: ' . $url . '<br/>          
        Message: ' . nl2br($comment) . '<br/>
        Response: ' . $response . '<br/>';
        echo $result = sendmail($to, $subject, $message, $from);
        if ($result==1){
            $result = 'Thank you! We have received your message.';
            header('location:index.php?result='.$result);
            ob_flush();
            exit();
        }else{
            $result = 'Sorry, unexpected error. Please try again later';
            header('location:index.php?result='.$result);
            ob_flush();
            exit();
        } 
    }

    function sendmail($to, $subject, $message, $from) {
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
        $headers .= 'From: ' . $from . "\r\n";

        $result = mail($to,$subject,$message,$headers);
        if ($result) return 1;
        else return 0;
    }
   ?>

Get_Captcha.php

    <?php
    session_start();
    $string = '';
    for ($i = 0; $i < 5; $i++) {
       $string .= chr(rand(97, 122));
    }

    $_SESSION['random_number'] = $string;
    $dir = 'fonts/';
    $image = imagecreatetruecolor(165, 50);
    $num = rand(1,2);
    if($num==1)
    {
       $font = "Capture it 2.ttf"; 
    }
    else
    {
       $font = "Molot.otf";
    }
    $num2 = rand(1,2);
    if($num2==1)
    {
       $color = imagecolorallocate($image, 113, 193, 217);
    }
    else
    {
       $color = imagecolorallocate($image, 163, 197, 82);
    }

    $white = imagecolorallocate($image, 255, 255, 255); 
    imagefilledrectangle($image,0,0,399,99,$white);
    imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font,          $_SESSION['random_number']);
    header("Content-type: image/png");
    imagepng($image);
    ?>

【问题讨论】:

    标签: php jquery html post


    【解决方案1】:

    问题是您正在检查您的验证码是否模糊:

    onblur="checkcode(this.value)"
    

    意味着只有当光标从验证码字段移动到其他东西(如按钮)时才会执行代码。

    您可以改为在表单的 onsubit 事件上应用如下内容:

    <form ... onSubmit="return checkcode()" >
    

    但在这种情况下,您的函数检查码还需要返回 true 或 false 才能使其工作。

    function checkcode(value){
            $('#wrong').hide();
            $.post( "contact.php",{ namevalue: "code" }, function( data ) {     
                var returndata = data;      
            if(returndata == value){    
                return true;        
            } else {            
                $('#wrong').show();
                $('#code').val('');  
                return false;       
            }  
        });
    

    这里有更多关于表单验证的信息:HTML/Javascript: Simple form validation on submit

    【讨论】:

      【解决方案2】:

      我没有尝试过这段代码,只是根据我的逻辑 我认为如果您使用单独的页面进行captcha 检查会更好

      function checkcode(value)
      {
          $('#wrong').hide();
          $.post( "captchaCheck.php",{ namevalue: "code" }, 
          function( data )
          {     
              var returndata = data;      
              if(returndata != value){  
                  $('#wrong').show();
                  $('#code').val('');           
              }
          });
      }
      

      captchaCheck.php

      <?php 
      ob_start();
      session_start();
      if(isset($_REQUEST['namevalue']) == $_SESSION['random_number']){
          echo $_SESSION['random_number'];
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-23
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        相关资源
        最近更新 更多