我如何验证thank.php和check.php? [关闭]

问题描述 投票:-1回答:1

我一直在使用PHP创建联系表单,其流程从index.php->check.php->thanks.php开始我使用POST方法并尝试验证thank.php和check.php,并希望确定没有人可以发送直接在开发人员工具上键入的“值”,并传送到thank.php。

目标:验证thank.php和check.php。

您能帮我吗?

代码如下:

check.php↓

<?php
    require_once("validation.php");

    if($_SERVER["REQUEST_METHOD"] !== "POST" && $post["button"] !== "input"){
        header("location: index.php");
    exit;
    }

    $post = $_POST;

    $errors = isValidation($post);

    if(!empty(array_filter($errors)) || $post["button"] === "return"){
        $post["flag"] = "input";
    }else{
        $post["flag"] = "confirm";
    }
    ?>

<!DOCTYPE html>
<html lang="ja">
<head>
</head>
<body>
    <?php if ($post["flag"] === "input") : ?>
        <form action="" method="POST">
                <div class="content">
                    Name: <input type="text" name="last_name" value="<?= isset($post["last_name"]) ? htmlentities($post["last_name"], ENT_QUOTES, "UTF-8") : ""?>">
                    <?php if(!empty($errors["last_name"])) : ?>
                    <p style="color: red;"><?php echo htmlentities($post["last_name"], ENT_QUOTES, "UTF-8") ?></p>
                    <?php endif; ?>
                </div>

    <?php if($post["flag"] === "confirm") :?>
        <form action="" method="POST">
            <div class="content">
                Name :
                <input type="hidden" name="last_name" value="<?= $post["last_name"] ?>">
                <p><?php echo htmlentities($post["last_name"], ENT_QUOTES, "UTF-8") ?></p>
            </div>

            <div class="content-button">
                <button type="submit" name="button" formaction= "" value="return" class="submit">Return</button>
                <button type="submit" name ="button" formaction= "thanks.php" value="submit" class="submit">Submit</button>
            </div>
        </form>
        <?php endif; ?>
</body>
</html>

validation.php↓

<?php
function isValidation($post) :array{

    if(empty($post["last_name"])){
        $errorMessage["last_name"] = "※Name is required";
    }

       return $errorMessage;
    }

thanks.php↓

<?php
    require_once("validation.php");

    $post = $_POST;
    $errors = isValidation($post);

    if($_SERVER["REQUEST_METHOD"] !== "POST" || $post["button"] !== "submit"){
    header("location: index.php");
    }


    $email = isset($post['email']);
    $subject = "Thank you for your enquiry!\r\n";

    $message = "Mr./Ms.{$post["last_name"]} {$post["first_name"]}\r\n\r\n".
               "The inquiry below has been sent!\r\n".
               "Name: Mr./Ms. {$post["last_name"]}\r\n".

    $headers = "From: [email protected]\r\n";


    mb_language("Japanese");
    mb_internal_encoding("UTF-8");
    if(mb_send_mail($email, $subject, $message, $headers)){
        echo "successfully sent\n";
    }else{
        echo "fail to send";
    }
    ?>

<!DOCTYPE html>
<html lang="ja">
    <head>
    </head>
    <body>
        <form>
            <h1>Your enquiry has been sent!</h1>
            <div class="content">
            <p>Thank you very much!</p>
            </div>
        </form>
    </body>
</html>
php security post
1个回答
1
投票

您不能阻止人们使用“开发人员工具”之类的浏览器实用程序。浏览器接收的所有HTML都是客户端代码,在客户端的计算机上执行,因此可以由客户端修改。

也许您问错了问题。正确的问题可能是“如何验证客户端发送的数据?”

[通常来说,对于收到的每个值,您都要执行服务器端(PHP)检查,以查看它是否符合您的预定标准,并使其检查尽可能严格。例如,如果您应该接收一个日期值,那么您的验证代码应检查输入是否为您期望的特定格式的有效日期。然后可能会有业务逻辑可用于进一步收紧标准,例如确保日期也不是10年前。

[不幸的是,关于一个人的名字的验证并不多,因为在法律上它绝对可以是任何东西,因此您只能trim()该字段,然后检查它是否不为空。对于电子邮件,您可以使用filter_var()FILTER_VALIDATE_EMAIL

尽管我也建议在该表单中添加一个验证码,否则您将收到很多垃圾邮件。

© www.soinside.com 2019 - 2024. All rights reserved.