Cancel all $ _POST variables so that I don't get a warning field when redirecting

After submitting the form, I have 2 $ _POST variables. On the landing page, I want to turn them into $ _SESSION variables and disable $ _POST so that the user does not receive a warning that the browser will send its 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 repeat the message, it won’t show anything, but every time I reboot, I get a browser warning again.

+6
source share
4 answers

You cannot just delete $ _POST data from the server. The browser warns about this because it is stored in the browser. If he resubmit the data, he will send it back to the server and close $_POST again.

What you need to do is use the POST-REDIRECT-GET template.

Process the POST data (for example, dropping it into a session), and then send the browser a redirect, not an HTML document. Then the browser makes a GET request to the server, and you give them a page.

+8
source

It's impossible. When the user presses reload, the PREVIOUS action is redone, which was the message. Even if you disabled POST values ​​when the form was first submitted, the reload action will be a completely new request. The browser will ALWAYS ask the user if they want to resend the data.

To prevent this, you should redirect the user to another page AFTER the formation of the message.

eg.

  • The user receives the form.
  • user submits form
  • REDIRECTs server user to a new page
  • user GETS a new page
  • user clicks reload
  • the user again adds a new page - POST is not executed.
+4
source

This will not work, because the browser does not warn you about the server side, so it asks you to confirm that it must perform an unsafe action.

In most cases, you should use the POST / Redirect / GET pattern, which does not suffer from this problem.

+2
source

his old post, but I also ran into the same problem, so here is a simple solution,

  • create login.php ==> which send two variables via the html form

  • create getHome.php ==> which will receive two variables $ _POST ['myvar']; function and put these two published variables into the session, as well as echo wrong user n pass here if you want.

  • create home.php ==> here you set the if condition to verify the session, that is, if (isset ($ _ SESSION ['myvar'])) {display home} else {header ('location: login.php'); * the header function will redirect you to login.php if you do not receive the home.php file directly without entering.

its done !!

+1
source

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


All Articles