Recommended Free Tools
To redirect after a PHP form submission, handle the POST request before producing any page output, process and validate the submitted values, send a Location header, and stop the script with exit;. For example:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Validate and process the submitted form here.
header('Location: /thank-you.php', true, 303);
exit;
}
?>
This sends the browser to /thank-you.php with an explicit 303 response. The destination is an example; replace it with the page you want to show after processing.
Where should the redirect code go?
Put the POST-handling branch at the start of the PHP request, before the page template, HTML, whitespace, or any included file that might output content. PHP’s header() manual requires headers to be sent before actual output. Even a blank line outside PHP tags or output from an included file can prevent the redirect header from being sent.
A common pattern is to process the form at the top of the same PHP file and render the form later for non-POST requests:
#1 Best Overall
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
// Validate $name and perform the intended processing.
header('Location: /thank-you.php', true, 303);
exit;
}
?>
<!doctype html>
<html lang="en">
<body>
<form method="post">
<label>Name
<input name="name">
</label>
<button type="submit">Send</button>
</form>
</body>
</html>
PHP makes submitted form values available to the script through request variables such as $_POST; see PHP’s documentation on variables from external sources. Validate the values and complete the operation the form is meant to perform before redirecting. The example is only a placement illustration, not complete validation for a particular form.
Should you use a 302 or 303 redirect?
For a redirect after handling a POST, an explicit 303 is a clear choice when the browser should make a separate request for the destination page. PHP’s header() function accepts a response-code argument. Without an explicit code, a Location header normally results in a 302 response, unless a 201 or 3xx status has already been set. The PHP manual describes this behavior and the available response-code handling in its header() reference.
Rank #2
Use the compact form when PHP’s default status is appropriate:
header('Location: /thank-you.php');
exit;
If the script has already selected a response status, check that choice as well; PHP documents http_response_code() as another way to set the status. Choose the status deliberately rather than assuming every redirect is automatically a 303.
Free tools Windows power users keep installed
One-click scans. No signup required.
How do you choose the redirect destination?
A root-relative path such as /thank-you.php points to a location on the same site, regardless of the current page’s directory. A relative path such as thank-you.php is interpreted in the context of the requested URL, which may lead somewhere other than intended. PHP’s manual notes that contemporary clients generally accept relative URIs in Location, while older clients may require an absolute URI.
If the form submission should send the visitor to another site, use a known, intentional absolute URL. Do not copy an unchecked destination from a submitted field into the Location header; use a fixed target or validate the target against an allowlist.
Quick Recap
Rank #4
What to check if the redirect fails
- “Headers already sent” or “Cannot modify header information”: Move the redirect branch before all output. Check for HTML, whitespace outside PHP tags, and included files that print content.
- The destination is wrong: Check whether the path is relative to the current URL. Use an intentional root-relative path for a same-site page, or a known absolute URL for an external destination.
- Code after the redirect still runs: Put
exit;immediately afterheader()so the current request handler stops. - The response status is unexpected: Check whether an earlier part of the script set a status code. Pass an explicit response code to
header()when needed, or review the use ofhttp_response_code().
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

