开发者

How to validate forms when a user changes the name of an input element in firebug?

I realize it's impossible to prevent someone from changing the names of input elements in firebug.

How should I approach this problem? A user changes the name of the input element "firstname" to "month" and visa versa.

<form action="example.php" method="post" enctype="multipart/form-data">
    <table  border='2'>
        <tr>
            <td>
                F开发者_开发问答irst name: <input type="text" name="firstname" /><br />
            </td>
        </tr>
        <tr>
            <td>
                Last name: <input type="text" name="lastname" /><br />
            </td>
        </tr>
        <tr>
            <td>Birth Month:
                <select name="month">
                    <option value="01">January</option>
                    <option value="02">February</option>
                    <option value="03">March</option>
                    <option value="04">April</option>
                    <option value="05">May</option>
                    <option value="06">June</option>
                    <option value="07">July</option>
                    <option value="08">August</option>
                    <option value="09">September</option>
                    <option value="10">October</option>
                    <option value="11">November</option>
                    <option value="12">December</option>
                </select>
                <br />
            </td>   
        </tr>
        <tr>
            <td><input type="submit" name="Submit" value="Sign Up!" /></td>
        </tr>
    </table>
</form>

My best idea so far is:

<?php
$month= $_POST['month'];
if($month!= 01 || $month!= 02 ... $month!= 12)
    echo 'wrong month';
?>

However this won't be clean for the year of birth... Facebook does a great job of preventing this when you sign up but I wasn't able to figure out what they did. A non-javascript solution would be much appreciated.

EDIT: Lawrence, what do you recommend for a location form?


Going through each month in a condition is unneeded, php has functions for that.

<?php
$firstname= (string)$_POST['firstname'];
$lastname = (string)$_POST['lastname'];
$month    = $_POST['month'];

if(in_array($month,range(01,12))===true){
    $cont = TRUE;
}else{
    $cont = FALSE;
}
//If set, not empty, not swapped for month & greater or equals to 6 chars 
if(isset($firstname) && $firstname!="" && strlen($firstname) >=6 && in_array($month,range(01,12))===false){
    $cont = TRUE;
}
//If set, not empty, not swapped for month & greater or equals to 6 chars
if(isset($lastname) && $lastname!="" && strlen($lastname) >=6 && in_array($month,range(01,12))===false){
    $cont = TRUE;
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜