Google Login with Google

How to Fix PHP header() Redirect Not Working

1. Problem Description
The following code sometimes doesn't work as expected:
phpheader("Location: index.php");
👉 The page doesn't redirect, or an error is thrown.

2. Root Causes
(1) Output Has Already Been Sent
HTML, whitespace, or an echo statement was executed before the header() call.
(2) BOM (Byte Order Mark) Issue
The file's encoding includes an invisible BOM character at the start, which counts as output.
(3) Missing exit
Without exit, the script continues executing after the redirect header is sent.

3. Solutions
✔ No Output Before header()
php<?php
header("Location: index.php");
exit;
✔ Remove Leading Whitespace
php<?php
// There must be no spaces or blank lines before this opening tag
✔ Use Output Buffering (Alternative)
phpob_start();
header("Location: index.php");
exit;

4. Code Example
phpif ($login_success) {
header("Location: notice.php");
exit;
}

5. Summary

header() must be called before any output is sent
Always use exit immediately after header()
Even a single space before <?php can cause the error


💡 Pro Tip
The "Headers already sent" error message is your biggest clue — if you see it, look for any output (including whitespace or HTML) that appears before your header() call.
← Back to list
💬 Comments (1)
전혜주
전혜주 Apr 27, 2026 · 12:14
it's oftenly happening issues. Follow the advice above. Things will be solved. if have any further questions, leave a message hear!