簡體   English   中英

PHP-經過一些驗證后發送郵件

[英]PHP- Send Mail after some Validation

我是PHP新手,無論如何都不是專家。 無論如何,我正在構建一個PHP和HTML聯系人表單,並且在驗證字段輸入(修整,刪除,htmlspecchars ..)的方式上感到困惑。 無論如何,這是我的代碼,請放心,我對此很菜鳥。

<?php
// define variables and set to empty values
$name = $email = $web = $telephone = $pages = $completion_date = $update_option = $hosting_option = $domain_option = $text = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = test_input($_POST["name"]);
    $email = test_input($_POST["email"]);
    $web = test_input($_POST["web"]);
    $telephone = test_input($_POST["telephone"]);
    $pages = test_input($_POST["pages"]);
    $completion_date = test_input($_POST["completion_date"]);
    $update_option = test_input($_POST["update_option"]);
    $hosting_option = test_input($_POST["hosting_option"]);
    $domain_option = test_input($_POST["domain_option"]);
    $text = test_input($_POST["text"]);
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

$msg = $name . "\n";
$msg = $email . "\n";
$msg = $web . "\n";
$msg = $telephone . "\n";
$msg = $pages . "\n";
$msg = $completion_date . "\n";
$msg = $update_option . "\n";
$msg = $hosting_option . "\n";
$msg = $domain_option . "\n";
$msg = $text . "\n";

$recipient = "myemail@mydomain.com";
$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: <myemail@mydomain.com>, Reply-To: <myemail@mydomain.com>" . "\r\n";
$mailheaders = "Cc: <$email>" . "\r\n";

mail($recipient, $subject, $msg, $mailheaders);
?>

直到$ msg的定義都還不錯,您還是要覆蓋它。

在第一個等號(=)之前加一個點(。)

$msg = $name . "\n";
$msg .= $email . "\n";
$msg .= $web . "\n";
... etc

在這種情況下,不需要帶stripslasheshtmlspecialchars 畢竟,您不會輸出任何包含POSTed值的HTML。

表單驗證的問題是,例如,我可以在email字段中寫任何東西,並且仍然可以驗證。 您應該對所有字段進行個案驗證,例如,使用電子郵件字段

if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    // The email is valid
    $email = $_POST['email'];
}

等等。 如果您不需要他們成為他們所說的樣子,則可以忽略這一點。 除此之外,它看起來還不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM