title: How do you POST to a page using the PHP header() function?
body: <p>I found the following code on <a href="http://php.net/manual/en/function.header.php#89447" rel="noreferrer">here</a> that I think does what I want, but it doesn't work:</p>
  
  <pre><code>$host = "www.example.com";
  $path = "/path/to/script.php";
  $data = "data1=value1&amp;data2=value2";
  $data = urlencode($data);
  
  header("POST $path HTTP/1.1\r\n");
  header("Host: $host\r\n");
  header("Content-type: application/x-www-form-urlencoded\r\n");
  header("Content-length: " . strlen($data) . "\r\n");
  header("Connection: close\r\n\r\n");
  header($data);
  </code></pre>
  
  <p>I'm looking to post form data without sending users to a middle page and then using JavaScript to redirect them. I also don't want to use GET so it isn't as easy to use the back button.</p>
  
  <p>Is there something wrong with this code? Or is there a better method?</p>
  
  <p><strong>Edit</strong> I was thinking of what the header function would do. I was thinking I could get the browser to post back to the server with the data, but this isn't what it's meant to do. Instead, I found a way in my code to avoid the need for a post at all (not breaking and just continuing onto the next case within the switch).</p>