Php – Unset all $_POST variables so that I don’t get alert box upon redirect

formsphppost

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.

Best Solution

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.

Related Question