有更好的方法来验证我的表单吗? PHP

时间:2016-12-30 14:38:05

标签: php

我正在尝试创建一个注册页面,我想知道是否有更好的方法来做到这一点。所以我认为不是键入每个字段,或者这是一个很好的方法吗?

这就是我目前所拥有的。

这是表格

<form action="" method="POST">
    <input type="text" name="username" placeholder="Username:" />
    <input type="password" name="password" placeholder="Password:" />
    <input type="password" name="password" placeholder="Confirm password:" />
    <input type="text" name="email" placeholder="Email:" />
    <input type="text" name="first_name" placeholder="First name:" />
    <input type="text" name="last_name" placeholder="Last name:" />
    <input type="submit" name="register" value="REGISTER" />
</form>

这就是我对PHP方面的看法

function reg_validation() {    
     if ($_POST['register']) {   
        if (empty($_POST['username'])) {
            echo 'Please enter a username';
        }
        if (empty($_POST['password'])) {
            echo 'Please enter a password';
        }
        if (empty($_POST['confirm_password'])) {
            echo 'Please confirm your password';
        }
        if (empty($_POST['email'])) {
            echo 'Please enter an email address';
        }
        if (empty($_POST['first_name'])) {
            echo 'Please enter your first name';
        }
        if (empty($_POST['last_name'])) {
            echo 'Please enter your last name';
        }
    }
}

2 个答案:

答案 0 :(得分:1)

<?php

function reg_validation() {    
    if (isset($_POST['register'])) {
        $expectedFields = array(
            'username', 'password', 'confirm_password', 'email', 'first_name', 'last_name'
        );

        foreach ($expectedFields as $value)
            if (!array_key_exists($value, $_POST) || empty($_POST[$value]))
                return false;

        return true;
    }

    return false;
}

?>

不知道我在上一个答案中是否误解了你的问题,所以这是另一个建议。代码未经过测试,因此可能包含轻微错别字。

在这个建议中,我们将所有字段名称存储在一个数组中,并在数组中循环,并检查是否有任何字段名称不是post数组中的键。

因此,如果在post数据中找到所有字段,则此函数将返回true,否则返回false。

答案 1 :(得分:0)

<?php

if (isset($_POST['register'])) {
    try {
        if (empty($_POST['username']))
            throw new Exception('missing_username');
        else if (empty($_POST['password']))
            throw new Exception('missing_password');
        else if (empty($_POST['confirm_password']))
            throw new Exception('missing_confirm_pass');
        else if (empty($_POST['email']))
            throw new Exception('missing_email');
        else if (empty($_POST['first_name']))
            throw new Exception('missing_firstname');
        else if (empty($_POST['last_name']))
            throw new Exception('missing_lastname');

        // do something if all fields are filled
    } catch (Exception $e) {
        $errorArray = array(
            'missing_username' => 'Please enter a username',
            'missing_password' => 'Please enter a password',
            'missing_confirm_pass' => 'Please confirm your password',
            'missing_email' => 'Please enter an email address',
            'missing_firstname' => 'Please enter your first name',
            'missing_lastname' => 'Please enter your last name'
        );

        if (isset($errorArray[$e->getMessage()]))
            echo $errorArray[$e->getMessage()];
    }
}

?>

我喜欢这样做,不知道别人怎么想。

通过使用try catch,我们不需要返回一个函数,因为我们可以抛出异常来从try部分中断。

相关问题