Current Location: Home> Latest Articles> How to Redirect Webpages Using PHP header Function: A Quick Guide to Page Redirection

How to Redirect Webpages Using PHP header Function: A Quick Guide to Page Redirection

gitbox 2025-08-07

In web development, page redirection is a common practice—such as redirecting users to their dashboard after login or forwarding an outdated URL to a new address. PHP provides a straightforward way to handle this using the header() function to send HTTP headers and instruct the browser to navigate to a new page.

1. Basic Syntax

The PHP header() function can be used to send raw HTTP header information. Its syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$header</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$replace</span></span><span> = </span><span><span class="hljs-literal">true</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$response_code</span></span><span> = </span><span><span class="hljs-number">0</span></span><span>);
</span></span>

When used for redirection, a common format is:

<span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">&#039;Location: https://www.example.com&#039;</span></span><span>);
</span><span><span class="hljs-keyword">exit</span></span><span>;
</span></span>

Here, Location is a special HTTP header that instructs the browser to redirect to another URL. Calling exit ensures that PHP stops executing any further code to prevent unwanted output or logical errors.

2. Common Use Cases

1. Redirect After Login

<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$login_success</span></span><span>) {
    </span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">&#039;Location: /dashboard.php&#039;</span></span><span>);
    </span><span><span class="hljs-keyword">exit</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Incorrect username or password."</span></span><span>;
}
</span></span>

2. Redirect After Successful Form Submission

<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$_SERVER</span></span><span>[</span><span><span class="hljs-string">&#039;REQUEST_METHOD&#039;</span></span><span>] === </span><span><span class="hljs-string">&#039;POST&#039;</span></span><span>) {
    </span><span><span class="hljs-comment">// Process form</span></span><span>
    </span><span><span class="hljs-comment">// ...</span></span><span>
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">&#039;Location: /thank-you.php&#039;</span></span><span>);
</span><span><span class="hljs-keyword">exit</span></span><span>;

}

3. Prevent Duplicate Form Submission (Post/Redirect/Get Pattern)

Using header() to redirect after form submission helps prevent data from being resubmitted if the page is refreshed:

<span><span><span class="hljs-comment">// Redirect after saving data</span></span><span>
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">&#039;Location: /success.php&#039;</span></span><span>);
</span><span><span class="hljs-keyword">exit</span></span><span>;
</span></span>

3. Notes and Considerations

  1. Call header before any output
    If any output has already been sent (such as echo, HTML tags, or even whitespace), using header() will result in an error: “Cannot modify header information.” You can avoid this by enabling output buffering with ob_start().

  2. Use absolute paths for safety
    Although you can use relative paths like Location: dashboard.php, it's recommended to use absolute paths or full URLs to avoid path-related errors.

  3. Use HTTP status codes appropriately
    You can set the HTTP status code using the third parameter of header(). For example:

    <span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">&#039;Location: /new-page.php&#039;</span></span><span>, </span><span><span class="hljs-literal">true</span></span><span>, </span><span><span class="hljs-number">301</span></span><span>); </span><span><span class="hljs-comment">// Permanent redirect</span></span><span>
    </span></span>

    Status code meanings:

    • 301: Permanent Redirect

    • 302: Temporary Redirect (default)

    • 303: Recommended for redirect after form submission

4. Conclusion

Using header('Location: ...') is one of the most straightforward and effective ways to handle page redirection in PHP. Mastering this allows you to easily manage navigation, state transitions, and user experience optimizations. In real-world projects, combining it with appropriate business logic and HTTP status codes helps you implement redirection strategies that align with web standards.