2013-09-30 47 views
0

好的,請在這裏忍受。recaptcha PHP驗證無法正常工作我認爲

我有我的HTML表單代碼設置這樣的...

<script type="text/javascript"> 
    var RecaptchaOptions = { 
     theme : 'clean' 
    }; 
</script>         
<form method="post" action="signup-form-1.php"> 
    <label for="Name">Name:</label><br /> 
    <input type="text" name="Name" id="Name" placeholder="Your Name" required="required" /><br /><br /> 

    <label for="Email">Email Address:</label><br /> 
    <input type="email" name="Email" id="Email" placeholder="Your E-mail address" required="required" /><br /><br /> 

    <label for="Phone">Phone Number</label> 
    <input type="tel" name="Phone" id="tel" placeholder="Numbers and Hyphens allowed" required="required" /><br /><br /> 

    <!--<label for="Subject">Phone Number:</label><br /> 
    <input type="text" name="Subject" id="Subject" /><br /><br />--> 

    <label for="Age">Age:</label><br /> 
    <input type="text" name="Age" id="Age" placeholder="How Old Are You?" required="required" /><br /><br /> 

    <label for="InterestedIn">What Tournament are you interested in?</label><br /><br /> 
     <input type="checkbox" name="InterestedIn[]" value="op1"><p class="check1">option 1</p> 
     <input type="checkbox" name="InterestedIn[]" value="op2"><p class="check1">option 2</p> 
     <input type="checkbox" name="InterestedIn[]" value="op3"><p class="check1">option 3</p> 
     <input type="checkbox" name="InterestedIn[]" value="op4"><p class="check1">option 4</p> 


    <label for="Message">Message:</label><br /> 
    <textarea name="Message" rows="20" cols="20" id="Message" placeholder="Additional Information"></textarea><br /><br /> 

    <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=recaptcha key 1"></script> 
    <noscript> 
     <iframe src="http://api.recaptcha.net/noscript?k=recaptcha key 2" height="300" width="500" frameborder="0" title="CAPTCHA test"></iframe> 
     <br /> 
     <label for="tswcaptcha">Copy and paste the code provided in above box here:</label><br /> 
     <textarea name="recaptcha_challenge_field" id="tswcaptcha" rows="3" cols="40"></textarea> 
     <input type="hidden" name="recaptcha_response_field" value="manual_challenge" /> 
    </noscript> 

    <input type="submit" name="submit" value="Submit" class="submit-button" /> 
    <input type="reset" name="reset" value="Reset" class="reset-button" /> 

</form> 

我的驗證碼顯示出來,我能夠輸入的話,我有我的公鑰和私鑰在正確的景點和一切。到現在爲止還挺好。

我的PHP代碼看起來是這樣的....

<?php 


$EmailFrom = Trim(stripslashes($_POST['Name'])); 
$EmailTo = "[email protected]"; 
$Subject = 'Custom Subject Line here'; 
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Phone = Trim(stripslashes($_POST['Phone'])); 
$Age = Trim(stripslashes($_POST['Age'])); 
$Message = Trim(stripslashes($_POST['Message'])); 
$InterestedIn = Trim(stripslashes($_POST['InterestedIn'])); 


// prepare email body text 
$Body = ""; 
$Body .= "Name: "; 
$Body .= $Name; 
$Body .= "\n"; 
$Body .= "Email: "; 
$Body .= $Email; 
$Body .= "\n"; 
$Body .= "Phone: "; 
$Body .= $Phone; 
$Body .= "\n"; 
$Body .= "Age: "; 
$Body .= $Age; 
$Body .= "\n"; 
$Body .= "Message: "; 
$Body .= $Message; 
$Body .= "\n"; 
$Body .= "InterestedIn: "; 
$Body .= implode(", ", $_POST['InterestedIn']); 
$Body .= "\n"; 


// send email 
$success = mail($EmailTo, $Subject, $Body, "From: $Name"); 



// redirect to success page 
if ($success){ 
    print "<meta http- 

equiv=\"refresh\" content=\"0;URL=signup-thanks.htm\">"; 
} 
else{ 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=signup-error.htm\">"; 
} 

?> 

我的PHP現在分析數據和電子郵件格式發送。我可以收到它,我甚至可以使用逗號分隔複選框選項,一切都可以。

唯一的問題我得到的是,即使如果recaptcha沒有填寫,表格仍然發送。我明白我需要驗證我的PHP文件中的recaptcha。問題是我不知道該怎麼做。

我只是一個網絡開發的學生在這一點上,我嘗試了一些我從來沒有嘗試過的東西 - 我們已經顯示BASIC聯繫表單和基本的PHP,但我們還沒有處理複選框,內爆或驗證 - 迄今爲止,我已經完全明白了這一點。

有人可以看看我的代碼,並告訴我如何將驗證函數放在我的PHP文件中?細節和細節非常受歡迎。

我已經試過在過去幾天自己搜索這些信息,但我仍然不明白 - 我無法修改我找到的適合我需求的答案。我還在學習。但我確實需要這樣做,您的幫助將不勝感激。

任何一種願意願意在渴望n00b上可憐的人?

+0

請至少[格式和縮進(http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code在發佈您的問題之前,您的代碼塊才能正確填寫。這會讓我們更容易閱讀和理解您的代碼。 –

回答

0

要解決HTML端的這個問題,你可以使用required attr。

例子:

<input type="hidden" name="recaptcha_response_field" value="manual_challenge" required/> 

爲了解決在PHP端另一個字符串控制這個問題,你可以使用isset和

例子:

if(isset($_GET['recaptcha_response_field']) && $_GET['recaptcha_response_field'] != "" && strlen($_GET['recaptcha_response_field']) > 5) 
1

您需要的ReCaptcha代碼向上頂,根據this page

嘗試此(未測試):

<?php 
    require_once('recaptchalib.php'); 
    $privatekey = "your_private_key"; 
    $resp = recaptcha_check_answer ($privatekey, 
           $_SERVER["REMOTE_ADDR"], 
           $_POST["recaptcha_challenge_field"], 
           $_POST["recaptcha_response_field"]); 

    if (!$resp->is_valid) { 
    // What happens when the CAPTCHA was entered incorrectly 
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . 
     "(reCAPTCHA said: " . $resp->error . ")"); 
    } else { 

     $EmailFrom = Trim(stripslashes($_POST['Name'])); 
     $EmailTo = "[email protected]"; 
     $Subject = 'Custom Subject Line here'; 
     $Name = Trim(stripslashes($_POST['Name'])); 
     $Email = Trim(stripslashes($_POST['Email'])); 
     $Phone = Trim(stripslashes($_POST['Phone'])); 
     $Age = Trim(stripslashes($_POST['Age'])); 
     $Message = Trim(stripslashes($_POST['Message'])); 
     $InterestedIn = Trim(stripslashes($_POST['InterestedIn'])); 


     // prepare email body text 
     $Body = ""; 
     $Body .= "Name: "; 
     $Body .= $Name; 
     $Body .= "\n"; 
     $Body .= "Email: "; 
     $Body .= $Email; 
     $Body .= "\n"; 
     $Body .= "Phone: "; 
     $Body .= $Phone; 
     $Body .= "\n"; 
     $Body .= "Age: "; 
     $Body .= $Age; 
     $Body .= "\n"; 
     $Body .= "Message: "; 
     $Body .= $Message; 
     $Body .= "\n"; 
     $Body .= "InterestedIn: "; 
     $Body .= implode(", ", $_POST['InterestedIn']); 
     $Body .= "\n"; 


     // send email 
     $success = mail($EmailTo, $Subject, $Body, "From: $Name"); 



     // redirect to success page 
     if ($success){ 
      print "<meta http- 

     equiv=\"refresh\" content=\"0;URL=signup-thanks.htm\">"; 
     } 
     else{ 
      print "<meta http-equiv=\"refresh\" content=\"0;URL=signup-error.htm\">"; 
     } 
    } 
?>