header('location: ..') not working

header('location: ..') not working

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Silent Killer: Why header('Location: ...') Fails in PHP

When debugging a single-page application where navigation relies entirely on URL-encoded GET requests to index.php, encountering a silent redirect failure is frustrating. Your logic dictates that the page loads, evaluates form data, and then issues a header command like header('Location: /index.php?page=login'). Yet, the browser remains stuck on the current URL, displaying no change whatsoever.

The culprit in this specific scenario is rarely the header() function itself or the syntax of the redirect string. Instead, it is almost invariably an output buffering issue triggered by invisible whitespace preceding the header command. In PHP, sending headers requires a pristine state before any byte is written to the output stream. If even a single character escapes before the redirect logic executes, the HTTP protocol forbids modifying the Location header, and PHP silently ignores the request.

The provided code snippet demonstrates a common pattern where form processing happens immediately after validation checks. The logic flow looks correct: check for POST data, validate the session connection, determine the path choice, and then issue the redirect. However, the execution stops before reaching the header() call if an error occurs earlier in the chain.

In the specific case described, the redirection fails not because of the redirect logic itself, but due to a blank space appearing after a ?> tag elsewhere in the file. This is a classic whitespace trap. When PHP encounters a closing tag ?>, any content following it on the same line—including spaces, tabs, or newlines—is interpreted as output. Even if that code block appears logically separate from your form handler, the moment PHP parses the file and hits that trailing space after the closing tag, the output buffer is flushed immediately upon script termination (or at the next echo/print statement).

To catch this elusive error without guessing where the whitespace resides, you must force PHP to reveal its output behavior. Adding these lines at the very top of your script is essential:

error_reporting(E_ALL);
ini_set('display_errors', 1);

With error reporting enabled, PHP will emit warnings whenever it attempts to send a header after output has already started. If you run into this issue in a production environment where display_errors is disabled, the script will simply exit without warning, but you can verify the state of the headers using headers_sent() before attempting any redirects:

if (headers_sent()) {
    die("Headers already sent! Check for whitespace after <?php or ?> tags.");
}

header('Location: /index.php?page=login');
exit();

The exit() call is critical here. Once a header is sent, the script must stop immediately to prevent subsequent code from generating output that would break the redirect chain. Without exit(), if the rest of your script contains debugging echo statements or template rendering logic, you might see mixed content errors or the redirect simply failing because the buffer was already contaminated by earlier output.

Another subtle pitfall involves the URL structure used in the redirect. The code attempts to redirect to /index.php?page=login. While valid, ensure that your server configuration does not strip query parameters unexpectedly or that the index.php file is actually located at the root path as expected. If the routing logic expects a relative path like page/login, using an absolute path starting with / changes the context entirely. Always verify that the target URL matches exactly what your entry point (index.php) expects to parse.

Finally, remember that this behavior is strict by design. The HTTP specification does not allow appending headers after the response body has begun. While modern frameworks often abstract this away, raw PHP remains unforgiving of whitespace. A single space after a closing tag can break your entire navigation flow, making header('Location: ...') appear to do nothing.

Our network