当用户的输入不正确时,如何防止绕过注册表?

时间:2014-08-24 03:35:11

标签: javascript php jquery

所以我试图制作我的第一张注册表格,并且我一直想弄明白我做错了什么。每当我输入错误或无效的输入或说出无效的用户名或电子邮件时,只要我填写表格,我仍然可以注册并将数据发送到我的数据库。我还是初学者,所以我不确定如何妥善安排他们或我缺少什么

这是我的代码:

<!doctype html>
<html>
<head>
<style>
form{
    margin: 0px auto;
    width: 470px;
    margin-top:150px;
}
label{
     width: 75px;
    display: inline-block;
}
.error{
    color: red;
}
</style>
</head>
<body>

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);

if(isset($_POST['username'])&&isset($_POST['password'])&&isset($_POST['email'])&&isset($_POST['age'])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $password_hash = md5($password);

    $age = $_POST['age'];
    $gender = $_POST['gender'];


    if(!empty($username)){
    if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
        $usernameErr = " Only letters and white space allowed"; 
        }
    if(!empty($password)){
    if(!empty($email)){
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
           $emailErr = " Invalid email"; 
         }
    if(!empty($gender)){
    if(!empty($age)){
        if(strlen($username)>30){
            $error = 'Please ahear to maxlength of fields.';
        } else {


            //MySQL database code goes here

        }


    } else {
        $ageErr = ' Select your age';
    }
    } else {
        $genderErr = ' Select your gender';
    }
    } else {
        $emailErr = ' Enter your email';
    }
    } else {
        $passErr = ' Enter your password';
    }   
    } else {
    $usernameErr = ' Enter your username';
    }
}
?>

        <span class="error"><?php echo $error?></span>
        <br><br>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" autocomplete="off">   
            <h2>Register</h2>
            <label for="username">Username</label>
                <input type="text" id="username" name="username" maxlength="50" value="<?php if(isset($username)){echo $username;}?>">
                <span class="error"><?php echo $usernameErr?></span>
                <br><br>
            <label for="password">Password</label>
                <input type="password" id="password" name="password">
                <span class="error"><?php echo $passErr?></span>
                <br><br>
            <label for="email">Email</label>
                <input type="email" id="email" name="email" maxlength="30" value="<?php if(isset($email)){echo $email;}?>">
                <span class="error"><?php echo $emailErr?></span>
                <br><br>
            <label>Gender</label>
                <input type="radio" name="gender" id="male" value="male">Male   
                <input type="radio" name="gender" id="female" value="female">Female
                <span class="error"><?php echo $genderErr?></span>
                <br><br>
            <label>Age</label>
                <select name="age" class="age">
                    <option value> </option>
                    <option value="18">18</option>
                    <option value="19">19</option>
                    <option value="20">20</option>
                    <option value="21">21</option>
                    <option value="22">22</option>
                    <option value="23">23</option>
                    <option value="24">24</option>
                    <option value="25">25</option>
                    <option value="26">26</option>
                    <option value="27">27</option>
                    <option value="28">28</option>
                    <option value="29">29</option>
                    <option value="30">30</option>
                </select>
                <span class="error"><?php echo $ageErr?></span>
                <br><br>
            <input type="submit" value="Register">
        </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情。有几种方法可以提高效率,但考虑到上述方法,我至少会按字段分解验证逻辑而不是巨大的嵌套if。因此,在下面的示例中,您设置一个主验证布尔值,基本上会说是提交,或者不是。当每种类型的字段进行验证时,它将确保在任何失败时将变量设置为false。然后在脚本的最后你做一个最后的if语句检查验证状态,如果它的好处去,然后做你的mysql表单提交的东西。如果没有,则继续处理并输出错误。另一个改变的是你的初始if语句(if(isset($_POST['username'])&&isset($_POST['password'])&&isset($_POST['email'])&&isset($_POST['age'])){。你真的只需要检查你是否正在尝试发布一个表单,因为每个字段都经过验证。希望这有帮助!

 <?php
    error_reporting(E_ERROR | E_WARNING | E_PARSE);

    // make sure that you have posted to the file
    if(count($_POST) > 0){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $password_hash = md5($password);
        $age = $_POST['age'];
        $gender = $_POST['gender'];

        // use a boolean value to catch errors
        $validated = true;

            // validate username
        if(!preg_match("/^[a-zA-Z ]*$/",$username)){
            $usernameErr = " Only letters and white space allowed";
            $validated = false;
        } else if(empty($username)){
            $usernameErr = ' Enter your username';
            $validated = false;
        }

            // validate username length
        if(strlen($username)>30){
            $error = 'Please ahear to maxlength of fields.';
        }

            // validate password
        if(empty($password)){
            $passErr = ' Enter your password';
            $validated = false;
        }

            // validate email
        if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = " Invalid email";
            $validated = false;
        } else if(empty($email)){
            $emailErr = ' Enter your email';
            $validated = false;
        }


        if(empty($gender)){
            $genderErr = ' Select your gender';
            $validated = false;
        }

        if(empty($age)){
            $ageErr = ' Select your age';
            $validated = false;
        }

        if($validated === true){
            echo 'im here';
                //MySQL database code goes here
        }
    }
?>

<!doctype html>
<html>
<head>
<style>
form{
    margin: 0px auto;
    width: 470px;
    margin-top:150px;
}
label{
     width: 75px;
    display: inline-block;
}
.error{
    color: red;
}
</style>
</head>
<body>
    <span class="error"><?php echo $error?></span>
    <br><br>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" autocomplete="off">
        <h2>Register</h2>
        <label for="username">Username</label>
            <input type="text" id="username" name="username" maxlength="50" value="<?php if(isset($username)){echo $username;}?>">
            <span class="error"><?php echo $usernameErr?></span>
            <br><br>
        <label for="password">Password</label>
            <input type="password" id="password" name="password">
            <span class="error"><?php echo $passErr?></span>
            <br><br>
        <label for="email">Email</label>
            <input type="email" id="email" name="email" maxlength="30" value="<?php if(isset($email)){echo $email;}?>">
            <span class="error"><?php echo $emailErr?></span>
            <br><br>
        <label>Gender</label>
            <input type="radio" name="gender" id="male" value="male">Male
            <input type="radio" name="gender" id="female" value="female">Female
            <span class="error"><?php echo $genderErr?></span>
            <br><br>
        <label>Age</label>
            <select name="age" class="age">
                <option value> </option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
            </select>
            <span class="error"><?php echo $ageErr?></span>
            <br><br>
        <input type="submit" value="Register">
    </form>
</body>
</html>