for form process what's better ? (secure/fast)
Layout Form :
<form id="myForm" name="frm1" action="insert.php" method="post">
<textarea name="content" id="content" cols="35" rows="5"></textarea>
<INPUT type=submit value="submit" name=submit></form>
Insert php :
<?php
if (( 'POST' == $_SERVER['REQUEST_METHOD'])) {
//php validation code
} else {
exit();
}
?>
Or
<?php
if (!isset($_POST['submit'])) {
//php validation code
} else {
exit();
}
?>
-
what form are you trying to process?Charming Prince– Charming Prince2011年12月12日 17:32:01 +00:00Commented Dec 12, 2011 at 17:32
-
I personally prefer the former. I don't know that there is a correct answerLevi Morrison– Levi Morrison2011年12月12日 17:39:31 +00:00Commented Dec 12, 2011 at 17:39
5 Answers 5
The second one, definitely. It's more readable. and even more logical
<?php
if (isset($_POST['submit'])) { //php validation code
//do something
}
else
{
exit();
}
?>
Comments
You should generally be checking whether or not the data exists that you are going to process. Along those lines, your second method is preferred, but don't assume people are going to click your submit button.
I have a couple other notes for you while I'm at it. You should really close your <input> tag with /> at the end of it.
Also, while you can make comparisons like ('POST' == $_SERVER['REQUEST_METHOD']), writing them in that order makes little sense. Flip it around like this: ($_SERVER['REQUEST_METHOD'] == 'POST')
Comments
Speed is irrelevant here. In terms of security these two cakes of code are diferents...
if (( 'POST' == $_SERVER['REQUEST_METHOD']))
{//php validation code
}
else
{exit();}
Here you are testing if the request method of your page is post, and then you do your validations.
if (!isset($_POST['submit']))
{//php validation code
}
else
{exit();}
Here you are testing if there is a value in the post values that has the key "submit". You are assuming that a field has this name, but that is not necessarily true. You can have post values with any field named "submit".
The real security concern here are your validation tests.
2 Comments
if (!isset($_POST['submit']))
{//php validation code
}
else
{exit();}
Second one makes more sense to me.
Comments
The second answer is the best, because you're only checking on your submit button. The other one is checking for just a post .