簡體   English   中英

jQuery PHP驗證碼驗證

[英]Jquery PHP Captcha Validation

我已經使用了驗證碼作為表格,如果我嘗試單擊提交(通過鼠標),即使在給出正確的驗證碼之后,我也會收到“錯誤的驗證碼”警告,而可悲的是如果我按下enter(通過鍵盤)即使輸入了錯誤的驗證碼,它也會顯示“謝謝”文本。

的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('');         
        }  
    });
  }

Contact.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);
    ?>

問題是您正在模糊檢查驗證碼:

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:提交時的簡單表單驗證

我沒有嘗試過這段代碼,只是用我的邏輯加重了,我認為如果您使用一個單獨的頁面進行驗證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'];
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM