Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unset all $_POST variables so that I don't get alert box upon redirect

Tags:

post

forms

php

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.

like image 912
Elaine Marley Avatar asked Mar 22 '12 16:03

Elaine Marley


3 Answers

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.

like image 151
Quentin Avatar answered Oct 24 '22 09:10

Quentin


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.

  1. user GETs the form.
  2. user POSTs the form
  3. server REDIRECTs user to a new page
  4. user GETs the new page
  5. user hits reload
  6. user GETs the new page again - no POST is performed.
like image 33
Marc B Avatar answered Oct 24 '22 08:10

Marc B


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.

like image 2
Jim Avatar answered Oct 24 '22 07:10

Jim