Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Redirect to Another Page After a PHP Form Submission

Process and validate the POST first, then send a PHP Location header and call exit before any page output.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

After processing a form’s POST request, redirect with PHP’s header('Location: /thank-you.php', true, 303); and immediately call exit;. Put this handler before any HTML or other output so PHP can still send the redirect headers.

Redirect after a PHP form submission

Handle the POST request at the start of the PHP script: validate and process the submitted values, send a Location header, then stop the request handler.

As an Amazon Associate I earn from qualifying purchases.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Validate and process the submitted form here.
    header('Location: /thank-you.php', true, 303);
    exit;
}
?>

The example sends the browser to a same-site thank-you page. The explicit 303 status is useful when a successful POST should be followed by a separate request to retrieve the destination page. See the PHP header() manual for the function’s header and response-code behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where to put the redirect

Place the POST-handling branch before the page template, layout include, HTML, whitespace outside PHP tags, or any other output. PHP’s manual says header() must run before actual output; even a blank line or output from an included file can prevent the redirect header from being sent.

PHP makes form-submitted information available to the script through external variables such as $_POST. Read and validate the relevant values before taking action; the PHP manual on variables from external sources describes how submitted data is made available.

Choose the redirect status and destination

Use an explicit 303 after POST

In header('Location: /thank-you.php', true, 303);, the third argument sets the response status. A 303 makes the follow-up request a retrieval of the destination page rather than continuing the form submission.

Use PHP’s default Location response when appropriate

If PHP’s default Location behavior is suitable, the shorter form is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
header('Location: /thank-you.php');
exit;

PHP documents that a Location header normally produces a 302 response unless a 201 or 3xx status has already been set. An earlier status selection can therefore affect the result; set the intended status explicitly when it matters. The same PHP manual page documents the response-code argument and refers to http_response_code().

Use a deliberate path

A root-relative destination such as /thank-you.php points to a path on the current site. A plain relative URI is interpreted in the context of the requested URL; PHP’s manual notes that older clients may require an absolute URI, even though most contemporary clients accept relative URIs.

Keep the destination fixed or choose it from an allowlist. Do not pass an unchecked form value into the Location header, where it could send visitors somewhere unintended.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Fix common redirect problems

  • “Cannot modify header information” or “headers already sent”: Move the redirect branch before output. Check for rendered HTML, whitespace outside PHP tags, and output from included files.
  • The redirect seems to happen, but later code still runs: Put exit; immediately after header() so the current script stops.
  • The browser goes to the wrong path: Check how the destination is written. Use an intentional root-relative path for a same-site destination, or a known absolute URL if the redirect should leave the site.
  • The response status is unexpected: Check whether the script set a status earlier. Pass the desired status to header() when you need to select it explicitly.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.