After I submit a form I have 2 $_POST variables. In the landing page I want to turn them into $_SESSION variables and unset the $_POST ones so that the user won't get the alert of the browser to send their data again. This is my (not working) code:
if(isset($_POST['clave']) && isset($_POST['email'])){
$_SESSION['id_user']=$_POST['clave'];
$_SESSION['email']=$_POST['email'];
}
if(isset($_SESSION['id_user']) && isset($_SESSION['email'])){
unset($_POST['clave']);
unset($_POST['email']);
//I do my stuff here
}
If I echo the posts nothing will show but everytime I reload I get the browser warning again.
You can't just delete the $_POST data from the server. The browser alerts it because it is stored by the browser. If it resubmits the data then it will send it back to the server and repopulate $_POST
.
What you need to do is to use the POST-REDIRECT-GET pattern.
Process the POST data (e.g. by dumping it into the session), then send the browser a redirect and not an HTML document. The browser then makes a GET request to the server and you give them the page then.
It's not possible. When the user hits reload, the PREVIOUS action is redone, which was a post. Even though you've unset the POST values when the form was first submitted, the reload action will be an entirely NEW request. The browser will ALWAYS ask the user if they'd like to resubmit the data.
To prevent this, you have to redirect the user to another page AFTER the post is formed.
e.g.
This won't work because the browser is not warning you about the server-side, it's asking for confirmation that it should perform an unsafe action.
In most cases, you should be using the POST/Redirect/GET pattern, which does not suffer from this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With