2

试图用php制作我自己的联系表格。有没有更好/更清洁的方法来解决这个问题?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Contact Form Practice</title>


</head>

<body>


<form method="POST" action="mailer.php">
Name:
<br>
<input type="text" name="name" size="19"><br>
<br>
Your Email Adress:
<br>
<input type="text" name="email" size="19"><br>
<br>
Message:
<br>
<textarea rows="9" name="message" cols="30"></textarea>
<br>
<br>
<input type="submit" value="Submit" name="submit">
</form>



</body>
</html>

----php---------------

<?php
if(isset($_POST['submit'])) {

$to = "mail@cheapramen.com";
$subject = "Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

} else {

echo "4! OH! 4!";

}
?>
4

5 回答 5

6

代码看起来是正确的,但我强烈建议添加一些数据验证。您需要确保所有必填字段都填写了有效信息。出于安全/可读性的目的,请务必对任何 HTML、JS 等进行编码/剥离。

最后,您还应该考虑使用 CAPTCHA 来防范垃圾邮件。我有一个运行与此类似的代码的旧站点,并且曾经每天收到超过 500 封垃圾邮件!

于 2009-12-13T23:40:26.200 回答
3

差不多就是这样,也许在成功完成后,您可以header()重定向到确认页面,但就处理表单而言,您所拥有的是非常标准的。

此外,您希望将数据清理为接受任何用户输入的标准做法。

您可能需要考虑实施 CAPTCHA 以防止机器人也敲击您的表单。

PHP 验证码

于 2009-12-13T23:41:57.563 回答
3

您绝对想做的一件事是让数据在电子邮件中发送更安全。我至少会在输入数据上运行 htmlentities 和 strip_tags,但你绝对应该考虑做进一步的验证。

也代替 isset($_POST["SUBMIT"]) 我可能会做类似...

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // body code here
}
于 2009-12-13T23:43:01.577 回答
3

我强烈建议您查找有关 PHPmail()劫持的一些信息,并确保您不会让您的脚本容易受到此类攻击。另外,其他人的建议也很好。

于 2009-12-13T23:51:28.003 回答
1

在问题中,您有 2 个单独的文件处理表单。问题是,如果您遇到验证错误,您别无选择,只能选择糟糕的“请单击后退按钮”解决方案。

考虑这个模板 PHP 文件,它将在一个页面上处理所有内容,提供数据验证、错误、重新提交和整个 9 码。

<?php

// Read input variables from _POST
$FormAction = (isset($_POST['FormAction']) ? $_POST['FormAction'] : '');
$FirstName = trim(isset($_POST['FirstName']) ? $_POST['FirstName'] : '');
...

// Define script variables
$Errors = array();

// Process input if data was posted.
switch($FormAction)
{ 
   case 'Process':
      // validation code

      if(empty($FirstName) or strlen($FirstName) > 20)
          $Errors[] = "First name is required.";

      ...

      if(count($Errors) > 0)
         break;

      // Here we have valid data..  Do whatever...


      // Now, redirect somewhere.
      header('Location: http://www.next.com/whatever');
      exit;

 }

 ?>
 <html>
    <body>
       <?php if(count($Errors)) { ?>
          <div class="Error">
              <?php foreach($Error as $Error) { ?>
                  <div><?php echo htmlspecialchars($Error); ?></div>
              <?php } ?>
          </div>
       <?php } ?>

       <form method="POST" action="<?php echo htmlspecialchars($_SERVER['REQUES_URI'], ENT_QUOTES); ?>" />
           <input type="hidden" name="FormAction" value="Process" />

           First Name: 
           <input type="text" name="FirstName" value="<?php echo htmlspecialchars($FirstName, ENT_QUOTES); ?>" />

           ...

           <input type="submit" />
       </form>

    </body>
 </html>
于 2009-12-14T00:23:02.003 回答