PHP header() redirect with POST variables [duplicate]

Active3 hr before
Viewed126 times

7 Answers


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.,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.,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., @Evan How is this any more secure? You will still need to validate the data after it's been posted, no way around it. – deceze ♦ Nov 26 '10 at 1:20

// from
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;
load more v

  header('Location: http://www.aaaa/index.php');
  exit();		// useless to let script running

  // permanent redirection (default is 302, 303 for most robots)
  header('Location: http://www.aaaa/index.php', true, 301);
  exit();		// useless to let script running

// or a meta in html header (if html generation is initiated)

   <meta http-equiv="Location" content="http://www.aaaa/index.php">
// or waits 15s before redirection

   <meta http-equiv="refresh" content="15;URL=http://www.aaaa/index.php">

// or via javascript
load more v

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., Access PHP variable in JavaScript [duplicate] 4 年前 , Get variable from PHP to JavaScript [duplicate] 4 年前 , python: changes to my copy variable affect the original variable [duplicate] 7 个月前

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']); ?>">

Use a PHP session. On the redirect page, start the session and assign the _POST data to the _SESSION:,Then on page3.php, restart the session and set the _SESSION data to the _POST. Then use as normal:,Is the header function the standard way to send form data to another page?,You can do header("location: page3.php&name=name ") which is much easier but that uses _GET rather than $_POST.

Please help me out. Thank you so much!

< ? php
   $errors = array();

   if (empty($_POST['name'])) {
      $errors['name'] = 'Please enter your name.';
   } else {
      $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);

   if (empty($_POST['employer'])) {
      $errors['employer'] = 'Please add your employer.';
   } else {
      $employer = filter_var($_POST['employer'], FILTER_SANITIZE_STRING);

   if ($name && $employer) { //	
      header("location: page3.php ");

<form name="myform" action="" method="post">

   <input type="text" name="name" value="" />
   <span class="warning"><?php if (isset($errors['name'])) echo $errors['name']; ?> </span>

   <input type="text" name="employer" value="" />
   <span class="warning"><?php if (isset($errors['employer'])) echo $errors['employer']; ?> </span>

   <input name="somename" type="hidden" value="somevalue" />
   <input type="submit" value="send">
load more v

While it might seem simple, when it comes to the header() function, the simplicity of the code can lead developers into a false sense of security. So let’s look at how you can use this function correctly.,Most guides will tell you that to make a PHP redirect you can just use the header() function at the top of your pages. To do that, you use the function to send a new URL, like this:,Both of these approaches will execute a little slower than an immediate header() redirect, but are arguably more flexible.,If you’ve been through our introduction to PHP 7.4, and our guide on how to build a website in 5 minutes, you’ll be aware that the header() function can be used to easily redirect a user to another page. In reality, though, using this function is not as simple as it seems. In this guide, we’ll show you how to make a PHP redirect that doesn’t cause big problems further down the line.

Most guides will tell you that to make a PHP redirect you can just use the header() function at the top of your pages. To do that, you use the function to send a new URL, like this:

header('Location: '.$newURL.php);
load more v

For the PHP redirect to work, the header() function must execute before any output is sent. That means, the code must be written above the <!DOCTYPE html> or <html> tags in your index.php file.,To redirect in PHP, you’ll first need to write your header() function, starting with header().,PHP redirects can help users and search engines navigate smoothly and securely between pages on your site. The good news is setting up these redirects is easy thanks to the header() function.,If you have issues with the header function, you can still set up a PHP redirect with JavaScript. While the redirect might be slower using JavasScript, it will still be effective. 

 < ? phpheader('Location:');
 exit; ? >
load more v

Tell browser not to cache the pages.,Content-Type declaration,You can prevent the browser to cache pages by using the following code:,You can redirect your user to some other page.

< ? php
header("Location:"); ?
load more v

Other "header-redirect" queries related to "PHP header() redirect with POST variables [duplicate]"