1

我是 PHP 新手,无论如何都不是专家。无论如何,我正在构建一个 PHP 和 HTML 联系表单,并且在验证字段输入(trim、strip、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);
?>
4

2 回答 2

1

在 $msg 的定义之前它看起来没问题,你继续覆盖它。

在第一个等号之后添加一个点 (.)

$msg = $name . "\n";
$msg .= $email . "\n";
$msg .= $web . "\n";
... etc
于 2013-10-01T19:04:20.427 回答
0

stripslashes在这种情况下htmlspecialchars是不必要的。毕竟,您不会输出任何包含 POST 值的 HTML。

您的表单验证的问题是,例如,我可以在电子邮件字段中写入任何内容,并且它仍然可以验证。您应该对所有字段进行逐个验证,例如使用电子邮件字段

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

等等。如果你不需要他们成为他们所说的那样,你可以省略这个。除此之外它看起来还可以。

于 2013-10-01T19:06:34.850 回答