Home » Php » html – PHP header() redirect with POST variables

html – PHP header() redirect with POST variables

Posted by: admin April 23, 2020 Leave a comment


I’m working with PHP, and I’m making an action page which a form posts to. The page checks for errors, then if everything is fine, it redirects them to the page where the data has been posted. If not, I need to to redirect them back to the page they were at with an error and the POST variables. Here is the gist of how it works.

The HTML would look like this…

<form name="example" action="action.php" method="POST">
  <input type="text" name="one">
  <input type="text" name="two">
  <input type="text" name="three">
  <input type="submit" value="Submit!">

action.php would look like this…

if(error_check($_POST['one']) == true){
    header('Location: form.php');
    // Here is where I need the data to POST back to the form page.
} else {
    // function to insert data into database
    header('Location: posted.php');

In the case of an error, I need it to POST back to the first page.
I can’t use GET, because the input will be too large.
I don’t want to use SESSION, if possible.
Is this possible?

How to&Answers:

If you don’t want to use sessions, the only thing you can do is POST to the same page. Which IMO is the best solution anyway.

// form.php


    if (!empty($_POST['submit'])) {
        // validate

        if ($allGood) {
            // put data into database or whatever needs to be done

            header('Location: nextpage.php');


<form action="form.php">
    <input name="foo" value="<?php if (!empty($_POST['foo'])) echo htmlentities($_POST['foo']); ?>">

This can be made more elegant, but you get the idea…


// from http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl
function do_post_request($url, $data, $optional_headers = null)
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  return $response;


It is not possible to redirect a POST somewhere else. When you have POSTED the request, the browser will get a response from the server and then the POST is done. Everything after that is a new request. When you specify a location header in there the browser will always use the GET method to fetch the next page.

You could use some Ajax to submit the form in background. That way your form values stay intact. If the server accepts, you can still redirect to some other page. If the server does not accept, then you can display an error message, let the user correct the input and send it again.


It would be beneficial to verify the form’s data before sending it via POST. You should create a JavaScript function to check the form for errors and then send the form. This would prevent the data from being sent over and over again, possibly slowing the browser and using transfer volume on the server.


If security is a concern, performing an AJAX request to verify the data would be the best way. The response from the AJAX request would determine whether the form should be submitted.


Use a smarty template for your stuff then just set the POST array as a smarty array and open the template. In the template just echo out the array so if it passes:

    header("Location: passed.php");
} else {
    $smarty->assign("variables", $_POST);

I have not tried this yet but I am going to try it as a solution and will let you know what I find. But of course this method assumes that you are using smarty.

If not you can just recreate your form there on the error page and echo info into the form or you could send back non important data in a get from and get it


echo $_GET[name];