PHP 新手,但取得新进展。我有一个联系表单,在用户提交后会发送电子邮件。现在我想通过删除坏字符或任何可能破坏电子邮件的内容来改进此表单。该电子邮件将由我阅读,因此从技术上讲我可以浏览所有垃圾邮件,但我不想这样做。我想要一封干净的电子邮件用于记录目的。
我使用 $_POST 数组,下面是联系表单的 HTML:
<form class="form" method="post" action="contact.php">
<p class="name">
<input type="text" name="name" id="name" />
<label for="name">Name</label>
</p>
<p class="email">
<input type="text" name="email" id="email" />
<label for="email">E-mail</label>
</p>
<p class="web">
<input type="text" name="web" id="web" />
<label for="web">Website</label>
</p>
<p class="telephone">
<input type="text" name="telephone" id="telephone" />
<label for="telephone">Telephone</label>
</p>
<p class="question">Preferred Contact Method</p>
<p id="preferred_question">
<input type="radio" name="contact_option" class="telephone_opt" value="Telephone" />
<label for="telephone_opt">Telephone</label><br />
<input type="radio" name="contact_option" class="email_opt" value="Email" />
<label for="email_opt">Email</label></p>
<p class="question">Describe what you need..</p>
<p class="text">
<textarea name="text"></textarea>
</p>
<p class="submit">
<input type="submit" value="Send" />
</p>
</form>
这是我的 contact.php (表单操作文件)
<?php
$msg = "Name: " . $_POST['name'] . "<br />";
$msg .= "Email: " . $_POST['email'] . "<br />";
$msg .= "Website: " . $_POST['web'] . "<br />";
$msg .= "Telephone: " . $_POST['telephone'] . "<br />";
$msg .= "Preferred Contact Method: " . $_POST['contact_option'] . "<br />";
$msg .= "Customer Needs: " . $_POST['text'];
$recipient = "[email protected]";
$subject = "Contact Has Been Made..";
$mailheaders = "MIME-Version: 1.0" . "\r\n";
$mailheaders .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$mailheaders .= "From: <[email protected]>" . "\r\n";
mail($recipient, $subject, $msg, $mailheaders);
echo "Thank You!";
?>
尝试这个函数作为一个很好的起点:
function cleanInput($input) {
// Pass the $_GET, $_POST or $_REQUEST array
$output = array();
foreach ($input as $key=>$value) {
$o = $value;
// Make sure it's within the max length
$o = substr($o,0,256);
// Tidy up line breaks
$o = preg_replace('/\n{2,}/', "\n\n", $o);
$o = nl2br($o,FALSE);
// Strip any odd characters
$o = preg_replace('/[^A-Za-z0-9\. -\!\?\(\)\<\>\@]/', "", $o);
// Put the data back in the array
$output[$key] = $o;
}
// Return the array
return $output;
}
Usage: $post = cleanInput($_POST);
然后更换$_POST
s with $post
: $_POST['name']
会成为$post['name']
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)