How to avoid resending data on refresh in php
Please use ob_start(); statement in first line itself.
How to prevent form resubmission when page is refreshed (F5 / CTRL+R)
Use the Post/Redirect/Get pattern. http://en.wikipedia.org/wiki/Post/Redirect/Get
With my website, I will store a message in a cookie or session, redirect after the post, read the cookie/session, and then clear the value of that session or cookie variable.
How to prevent resending data to the data base on reload?
You can perform a REDIRECT to a page with a message that the data was inserted.
and you can use this header function to do that:
header('Location: http://www.example.com/');
and also you can redirect to a different query. use this code:
header("Location: ?page=successfulsending");
Then you don't need if clause to check resending the POST data was sent. Just display the successful sending page if page is equal to successfulsending.
Stop page from resending forms on refresh
You could send a Location
header to a different URL in your script that is processing the form input. This new URL will be available for the browser without the need to send form data.
Don't allow to resend POST information after reload
The standard way of doing this is to redirect away from the POST endpoint, so that the browser does a GET which can be refreshed as much as it wants without resending the POST data.
If you do want to do it all on the POST endpoint, something like this should do it:
session_start();
if (!isset($_SESSION['bypass']) {
$_SESSION['bypass'] = mt_rand();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_POST['key'] != $_SESSION['bypass']) {
echo "Invalid key";
} else {
unset($_SESSION['bypass']);
echo "Hello";
}
} else {
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="key" value="'.$_SESSION['bypass'].'" /><input name="submit" type="submit" value="submit" /></form>';
}
Avoid resending forms on php pages
Use the Post-Redirect-Get Pattern.
- Accept a Post request
- Process the data
- Issue a redirect response
- Accept a Get request
- Issue a 200 response
If you need to display data from the submitted stuff, then include a row id or similar in (for example) the query string of the URL you redirect to.
Stop browsers asking to resend form data on refresh
You need to use the the POST-Redirect-GET pattern.
Make your form respond with a redirect to a GET request.
This way, when the user refreshes the page, it will only resend the GET.
How to avoid Page Reload Resend-Data message with PHP
You could redirect to a different query.
header("Location: ?page=thankyou");
Then, you don't even need to check if the POST data was sent. Just display the thank you page if page is equal to thank you.
Best way to avoid the submit due to a refresh of the page
Don't show the response after your create action; redirect to another page after the action completes instead. If someone refreshes, they're refreshing the GET requested page you redirected to.
// submit
// set success flash message (you are using a framework, right?)
header('Location: /path/to/record');
exit;
Related Topics
What Does the Dot-Slash Do to PHP Include Calls
Forcing Access to _Php_Incomplete_Class Object Properties
Codeigniter: 404 Page Not Found on Live Server
Utf8_(En|De)Code Removed from PHP7
Object of Class Stdclass Could Not Be Converted to String
How to Make a Catch-All Route in Laravel
Running Imagemagick on Xampp 1.8.3 with Windows 7
Designing a Secure Auto Login Cookie System in PHP
How to Access a Variable Across Two Files
Inserting Utf-8 Encoded String into Utf-8 Encoded MySQL Table Fails with "Incorrect String Value"
Execute Python Script from PHP
Basic Simplexml Working Example
Get "Content-Type" Header of Request in PHP
How to Detect If Have to Apply Utf-8 Decode or Encode on a String