2016-11-06 42 views
0

我想在我的自定義表單上使用蜜罐技術。 我的表單看起來像那樣。用於php表格的蜜罐技術

<form id="form-1" 
    action="<?php echo get_template_directory_uri(); ?>/mail.php" method="post" class="order__form form"> 
    <p class="form__title">Order and Receive 30% off</p> 
    <p class="form__text">fill out this form so you can get sale</p> 
    <input type="text" name="name" class="form__item" placeholder="Your name"> 
    <input type="email" name="email" required class="form__item" placeholder="Email address"> 
    <p class="robotic" id="pot"> 
     <label>If you're human leave this blank:</label> 
     <input name="robotest" type="text" id="robotest" class="robotest" /> 
    </p> 
    <input type="submit" value="Send" class="button form__button"> 
</form> 

輸入名稱robotest用於服務器端驗證。

這是mail.php代碼:

<?php 
    $mess = ''; 
    $mess .= '<hr>'; 
    if($_POST['robotest'] != ''){ 
     $error = "You are a gutless robot."; 
    } else { 
     if(isset($_POST['name'])) { 
      $name = substr(htmlspecialchars(trim($_POST['name'])), 0, 100); 
      $mess .= '<b>Имя отправителя: </b>' . $name . '<br>'; 
     } 
     if(isset($_POST['email'])) { 
      if($_POST['email']!=''){ 
       $email = substr(htmlspecialchars(trim($_POST['email'])), 0, 100); 
       $mess .= '<b>E-mail: </b>' . $email . '<br>'; 
      } 
     } 
    } 

    $mess .= '<b>Заявка пришла со страницы:</b> ' . $_SERVER["HTTP_REFERER"] .'<br>'; 
    $mess .= '<hr>'; 

    require 'class.phpmailer.php'; 

    $mail = new PHPMailer(); 
    $mail->AddAddress('xxx2xxx.com',''); 
    $mail->IsHTML(true); 
    $mail->CharSet = "UTF-8"; 
    $mail->Subject = "new"; 
    $mail->From = "new"; 
    $mail->FromName = "new"; 
    $mail->Body = $mess; 

    if ($mail->Send()) { 
     header('Location: ../'); 
    } else { 
     die ('Mailer Error: ' . $mail->ErrorInfo); 
    } 

    header("Location: /thanks/"); 

?> 

當我添加驗證爲robotest,該腳本不起作用。

+1

'這個腳本不工作'如何?空白頁?沒有信?電腦爆炸? –

+0

它正在工作,但未驗證。當機器人填寫robotest字段腳本時,主要想法必須是顯示錯誤。但現在每次提交和我收到電子郵件。 –

+0

因爲你的邏輯有缺陷。如果你縮進你的代碼 - 你會看到它。 –

回答

0

您正在設置$error變量,但您沒有在任何地方使用它。

如果你改變了:

$error = "You are a gutless robot."; 

要將:

die("You are a gutless robot."); 

你將有你描述你想擁有什麼。