0

I've been trying to create a simple login script, but for some reason the header() function has not been working. For example, consider the below code:

<form method="POST">
    <input type="submit">
</form>

<?php
if($_POST)
{
    header("Location: www.example.com");
    die();
}
?>

For some reason, this does not work (when a POST type submit button is clicked).

Having googled this issue, I've come to find that you cannot use the header() function with POST - you must use GET. However, using GET causes a query string to be shown in the URL, and this is not desirable when submitting login data.

What is the solution to this issue?

M Smith
  • 430
  • 1
  • 7
  • 19
  • "*cannot use the header() function with POST - you must use GET.*" That's not true. You just can't have any output prior to the `header()`-call. If you check your logs, I'm guessing you'll find something there :-) Also check out [How to fix “Headers already sent” error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php?rq=1) – Qirel Feb 15 '17 at 19:43
  • I dunno where you read that it doesn't work with POST. It definitely does. Most often it doesn't work because you output some whitespace before the PHP block. `header()` requires nothing be output before it is called. – Machavity Feb 15 '17 at 19:43
  • In this case, why not just use the `action=""` attribute? – Nytrix Feb 15 '17 at 19:48

0 Answers0