2017-04-13 75 views
0

上隨機驗證的問題,我有這種形式,我編碼,並想「人」的驗證,隨機循環通過一組的一個類似下面的代碼預定義的安全問題。接觸形式

此外,當表單被提交時,主題中的名稱會打印兩次。

我如何去實現呢?

<?php 
    if (isset($_POST["submit"])) { 
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $phone = $_POST['phone']; 
     $message = $_POST['message']; 
     $human = intval($_POST['human']); 
     $to = '[email protected]'; 
     $subject = 'Sent By '.$_POST['name']." ".$_POST['name']; 

     $body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message"; 
     // Check if name has been entered 
     if (!$_POST['name']) { 
      $errName = 'Please enter your name'; 
     } 

     // Check if email has been entered and is valid 
     if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
      $errEmail = 'Please enter a valid email address'; 
     } 

     //Check phone 
     if (!$_POST['phone']); { 
     } 

     //Check if message has been entered 
     if (!$_POST['message']) { 
      $errMessage = 'Please enter your message'; 
     } 
     //Check if simple anti-bot test is correct 
     if ($human !== 5) { 
      $errHuman = 'Your anti-spam is incorrect'; 
     } 
// If there are no errors, send the email 
if (!$errName && !$errEmail && !$errMessage && !$errHuman) { 
    if (mail ($to, $subject, $body, $from)) { 
     $result='<div class="alert alert-success">Thank You! We will be in touch</div>'; 
    } else { 
     $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again.</div>'; 
    } 
} 
    } 
?> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com."> 
    <meta name="author" content="BootstrapBay.com"> 
    <title>Bootstrap Contact Form With PHP Example</title> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> 
    </head> 
    <body> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
       <h1 class="page-header text-center">Contact Form Example</h1> 
       <form class="form-horizontal" role="form" method="post" action="form-test.php"> 
        <div class="form-group"> 
         <div class="col-sm-10"> 
          <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>"> 
          <?php echo "<p class='text-danger'>$errName</p>";?> 
         </div> 
        </div> 
        <div class="form-group"> 
         <div class="col-sm-10"> 
          <input type="email" class="form-control" id="email" name="email" placeholder="[email protected]" value="<?php echo htmlspecialchars($_POST['email']); ?>"> 
          <?php echo "<p class='text-danger'>$errEmail</p>";?> 
         </div> 
        </div> 
        <div class="form-group"> 

         <div class="col-sm-10"> 
          <input type="text" class="form-control" id="phone" name="phone" placeholder="Phone Number" value="<?php echo htmlspecialchars($_POST['phone']); ?>"> 
          <?php echo "<p class='text-danger'>$errPhone</p>";?> 
         </div> 
        </div> 
        <div class="form-group"> 

         <div class="col-sm-10"> 
          <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea> 
          <?php echo "<p class='text-danger'>$errMessage</p>";?> 
         </div> 
        </div> 
        <div class="form-group"> 
         <div class="col-sm-10"> 
          <input type="text" class="form-control" id="human" name="human" placeholder="2 + 3=?"> 
          <?php echo "<p class='text-danger'>$errHuman</p>";?> 
         </div> 
        </div> 
        <div class="form-group"> 
         <div class="col-sm-10 col-sm-offset-2"> 
          <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <div class="col-sm-10 col-sm-offset-2"> 
          <?php echo $result; ?> 
         </div> 
        </div> 
       </form> 
      </div> 
     </div> 
    </div> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
    </body> 
</html> 
+1

你先把它谷歌,然後根據你找到的代碼更新你的問題(如果它不工作,你有一個關於它是如何工作的問題等)。堆棧溢出不是代碼寫入服務。 –

+0

我做過谷歌它。另外,我不會要求任何人爲我寫代碼,我只是在尋找建議。不知道你如何到達,但無論如何感謝迴應。 – Dwayne360

回答

0

名稱是兩倍的問題,因爲你有自己的變量($_POST['name'])兩次在$subject變量:

$subject = 'Sent By '.$_POST['name']." ".$_POST['name']; 
//         ^^^^^^^^^^^^^^^^^^ 

應該

$subject = 'Sent By '.$_POST['name']; 


您可以輕鬆地在Google上搜索人工驗證碼。你有很多不同類型的人的驗證,基於圖像的,基於數學等

如果你要基於數學驗證碼我建議你使用這樣的事情(這主要是僞代碼):

<?php 
session_start(); 

$digit1 = mt_rand(1,100); 
$digit2 = mt_rand(1,100); 
$type = mt_rand(1,3); 

if($type === 1) { 
    $math = "$digit1 + $digit2"; 
    $_SESSION['answer'] = $digit1 + $digit2; 
} else if ($type === 2) { 
    $math = "$digit1 - $digit2"; 
    $_SESSION['answer'] = $digit1 - $digit2; 
} else { 
    $math = "$digit1 x $digit2"; 
    $_SESSION['answer'] = $digit1 * $digit2; 
} 
?> 
// your form 
<input type="text" class="form-control" id="human" name="human" placeholder="<?php echo $math; ?>"> 

然後,您可以比較來自人的答案和會話中的答案。

+0

謝謝錯過了。 – Dwayne360