Well, a good server should prevent _POST attacks automatically, at least mine does. However, server cannot detect userscript attacks.
So there is a little trick for that. This trick also makes spiderbots life a little harder, because form data is different and has to be manually copied from the source.
This solution works with $_SESSION
(read more).
This is the class.form_session.php example:
<?
// First we have to start the sessions all together
session_start();
// This wraps the functions neetly into a class
class FormSession {
// This function creates the form session
function create_form_session () {
$hash = md5('SomeRandomStringToMakeTheHashImpossible' . time());
return $_SESSION['form_session'] = $hash;
}
// This form simply returns the current form session
function current_form_session () {
return $_SESSION['form_session'];
}
// This function kills/deletes/unsets the form session
function destroy_form_session () {
unset($_SESSION['form_session']);
}
}
// Lets start the class and make it usable
$fs = new FormSession;
This is the user.php example:
<?
// Include START - If you are gonna use this trick cross-server, include this part to the top of each file (that use this method ofc)
require('class.form_session.php');
// Include END
// Lets create a new form session
$form_session = $fs->create_form_session();
// Lets generate a very simple form
print '<form method="post" action="test2.php">
<input type="text" name="' . $form_session . '_username" value="" placeholder="Username" /><br />
<input type="password" name="' . $form_session . '_password" value="" placeholder="Password" /><br />
<input type="submit" name="' . $form_session . '_submittrigger" value="Submit this!" />
</form>';
This is the success.php example:
<?
// Include START - If you are gonna use this trick cross-server, include this part to the top of each file (that use this method ofc)
require('class.form_session.php');
// Include END
// This catches the submit, this could also be in another file
if ($_POST[$fs->current_form_session() . '_submittrigger']) {
// Success!
echo '<pre>'; print_r($_POST); echo '</pre>';
// Now lets delete the session
$fs->destroy_form_session();
}
As you can see, this works in one file. Which is the way I personally like the submits to be, however if you include the top session related part to your success.php, then it should work fine.