2012-11-06 64 views
0

您好,我使用了一個基本的驗證碼腳本如下。我面臨的問題是,儘管錯誤的驗證碼值,表單已提交。如果Captcha的值輸入錯誤,我需要阻止提交表單。防止表單提交錯誤驗證碼

<?php 
    if(isset($_POST['submit'])) 
{ 
    $hash = (!empty($_POST['hash'])) ? preg_replace('/[\W]/i', '', trim($_POST['hash'])) : ''; // Remove any non alphanumeric characters to prevent exploit attempts 
    $captchacode = (!empty($_POST['captchacode'])) ? preg_replace('/[\W]/i', '', trim($_POST['captchacode'])) : ''; // Remove any non alphanumeric characters to prevent exploit attempts 
    // function to check the submitted captcha 
    function captchaChars($hash) 
    { 
    // Generate a 32 character string by getting the MD5 hash of the servers name with the hash added to the end. 
    // Adding the servers name means outside sources cannot just work out the characters from the hash 
    $captchastr = strtolower(md5($_SERVER['SERVER_NAME'] . $hash)); 
    $captchastr2 = ''; 
    for($i = 0; $i <= 28; $i += 7) 
    { 
     $captchastr2 .= $captchastr[$i]; 
    } 
    return $captchastr2; 
    } 
    if(!empty($captchacode)) 
    { 
    if(strtolower($captchacode) == captchaChars($hash)) // We convert submitted characters to lower case then compare with the expected answer 
    { 
     echo '<h3>The submitted characters were correct</h3>'; 

    } 
    else 
    { 
     echo '<h3>The submitted characters were WRONG!</h3>'; 
     return true; 
    } 

    } 

    else 
    { 
    echo '<h3>You forgot to fill in the code!</h3>'; 
    return true; 
    } 

} 
?> 
+0

請問您可以發佈表單處理器的完整代碼嗎?你發佈的內容不足以解決問題。 – drew010

回答

0

我已將Captcha啓動並在我的網站上運行,並且它沒有做任何事情來阻止垃圾郵件。它恐怕已經成爲一種垃圾郵件防範機制。

除此之外,如果驗證碼不正確並且不會提交表單,則需要返回false。

+0

我試過,但沒有工作.. – bhetus

+0

它陷入了哪種情況? –