2013-09-01 83 views
0

我這個[教程]提出的驗證碼[1],添加簡單的數學驗證碼

[1]:http://codechirps.com/how-to-add-a-completely-custom-captcha-to-any-web-form/,但在我看來,它不完整。 我做了代碼,但我可以發送電子郵件,即使我提出錯誤的答案。我覺得我必須在PHP文件中編寫額外的代碼,但我不知道在哪裏。任何幫助大大appriciated

<div class="modal-body"> 
       <form class="contact" name="contact"> 
        <label class="label" for="name">Имя</label><br> 
        <input type="text" name="name" class="input-xlarge"><br> 
        <label class="label" for="email">E-mail</label><br> 
        <input type="email" name="email" class="input-xlarge"><br> 
        <label class="label" for="message">Сообщение</label><br> 
        <textarea name="message" class="input-xlarge"></textarea> 
       </form> 
      </div> 
      <div class="modal-footer"> 
         <p>2 + 3 =</p> 
         <input type="text" name="captcha" /> 
       <input class="btn btn-warning" type="submit" value="Отправить" id="submit"> 
       <a href="#" class="btn btn-danger" data-dismiss="modal">Закрыть</a> 


<?php 
$myemail = ''; 
if (isset($_POST['name'])) { 
$name = strip_tags($_POST['name']); 
$email = strip_tags($_POST['email']); 
$message = strip_tags($_POST['message']); 
$captcha = check_input($_POST['captcha']); 
echo "<span class=\"alert alert-success\" >Сообщение отправлено</span><br><br>"; 

if (!preg_match("/5/", $captcha)) 
{ 
show_error("Check your math, Dude"); 
} 


$to = $myemail; 
$email_subject = "Contact form submission: $name"; 
$email_body = "You have received a new message. ". 
" Here are the details:\n Name: $name \n ". 
"Email: $email\n Message \n $message"; 
$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email"; 
mail($to,$email_subject,$email_body,$headers); 
}?> 
+1

那是因爲你沒有驗證'email'字段。而對於驗證碼..函數check_input做什麼?此外,您的驗證碼字段不在表單中。普韋凡德,謝謝你的回答。請告訴我,如果可能的話,我需要做些什麼來驗證電子郵件字段?我通過教程做了,並認爲check_input檢查captcha字段 – putvande

+1

PHP中檢查電子郵件的最簡單方法是做'filter_var($ _ POST ['email'],FILTER_VALIDATE_EMAIL)' – jdepypere

+0

請告訴我,我需要什麼把這個放在我的php文件 – Lucky

回答

1

好吧,所以你需要檢查你的輸入值,看看他們是否有效。如果沒有,則顯示錯誤並且郵件不會發送。如果所有的檢查都通過了,那麼這些女傭就會被送出去。所以你需要檢查$_POST['email']$_POST['captcha']字段(如果你想,檢查其餘是不是空的或其他)。

在PHP中,你可以做到這一點是這樣的:

$myemail = ""; 
if(isset($_POST['name'])){ // check if a form is submitted 
    if(empty(trim($_POST['name'])) || empty(trim($_POST['message'])) || empty(trim($_POST['email'])) || empty(trim($_POST['captcha']))){ // check if values are not empty 
     echo "Please fill in all the required fields."; 
    }else if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ // check email 
     echo "Please give a real e-mail address."; 
    }else if(!preg_match("/5/", $_POST['captcha'])){ // the code provided by your script 
     echo "Get your math right, dude!"; 
    }else{ // all fields seem to be ok 
     // sanitize input using htmlspecialchars(), see http://stackoverflow.com/a/5788361/1319187 
     $name = htmlspecialchars($_POST['email']); 
     $email = $_POST['email']; // email doesn't need to be sanitized since it's been filtered 
     $message = htmlspecialchars($_POST['message']); 

     //Send the mail 
     $to = $myemail; 
     $email_subject = "Contact form submission: $name"; 
     $email_body = "You have received a new message. ". 
      " Here are the details:\n Name: $name \n ". 
      "Email: $email\n Message \n $message"; 
     $headers = "From: $myemail\n"; 
     $headers .= "Reply-To: $email"; 
     if(mail($to,$email_subject,$email_body,$headers)){ 
      echo "Succes! Your mail has been sent!"; 
     }else{ 
      echo "Something went wrong with the sending of a mail."; 
     } 
    } 
} 

應該是相當簡單的,你可以在google的一些功能,如果你不知道他們在做什麼。我也不知道check_input()來自哪裏。這不是一個原生的PHP函數,你提供的鏈接不顯示它的功能。此外,檢查驗證碼的值是否爲5的正則表達式有點愚蠢,你可以檢查$_POST['captcha'] == '5'。另外請記住,你必須稍微隨機化這些值。

+0

仲裁者,謝謝你的回答!我只是將這段代碼添加到我的php中。但是我看到了錯誤,我錯了。我只知道一點PHP,對不起,你能提供整個代碼嗎?我將你的代碼放在echo「加密後

」; – Lucky

+0

用這個php替換所有的php。您可能需要將''放置在所需的樣式位置。 – jdepypere

+0

哦,我看到致命錯誤:無法在第4行的寫入上下文中使用函數返回值。我做錯了什麼?我把這段代碼放在$ myemail =「」行的下面。第4行if(empty(trim($ _ POST ['name']))|| empty(trim($ _ POST ['message']))|| empty(trim($ _ POST ['email']))|| empty (trim($ _ POST ['captcha']))){//檢查值是否爲空 – Lucky