Using a hash to check if a page with $ _POST values ​​has been updated

When submitting a form to the same PHP page, what is the correct method to find if the page was accidentally updated and not sent again?

Here is what I am using now:

$tmp = implode('',$_POST);
$myHash = md5($tmp);

if(isset($_SESSION["myHash"]) && $_SESSION["myHash"] == $myHash)
{
    header("Location: index.php");  // page refreshed, send user somewhere else
    die();
}
else
{
    $_SESSION["myHash"] = $myHash;
}

// continue processing...

Is there something wrong with this solution?

UPDATE: An example of this scenario would be inserting a row into a registration table. You would like this operation to be performed once.

+3
source share
3 answers

, . , , , , , . - .

<?php
    $token = /* a randomly generated string */;
    $_SESSION['_token'] = $token;
?>
<input type="hidden" name="_token" value="<?php echo $token; ?>" />

. , , POST.

Re comment: . , , SO, . , , .

. , .

, , POST, . , , . , . , ( ).

+2

, POST , ,

header("Location: new-page.php");

- .

+1

Using tokens in combination with a POST / REDIRECT / GET design pattern is the best solution available.

  • Installing a one-time token will prevent the user from clicking the "Back" button and again trying to submit the form.
  • Redirecting the user and using the view to display input allows them to remove updates if they wish.
+1
source

Source: https://habr.com/ru/post/1741376/


All Articles