2016-11-06 45 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."); 

你将有你描述你想拥有什么。