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.
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">'Location: https://www.example.com'</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.
<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">'Location: /dashboard.php'</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>
<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">'REQUEST_METHOD'</span></span><span>] === </span><span><span class="hljs-string">'POST'</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">'Location: /thank-you.php'</span></span><span>);
</span><span><span class="hljs-keyword">exit</span></span><span>;
}
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">'Location: /success.php'</span></span><span>);
</span><span><span class="hljs-keyword">exit</span></span><span>;
</span></span>
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().
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.
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">'Location: /new-page.php'</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
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.