You could use a function similar to:

function redirect($url) {
    header('Location: '.$url);
    die();
}

Worth noting, you should use a die() or exit() function to prevent further code execution.

Note that it just makes no sense to output large chunks of HTML if you are going to redirect. Therefore you have to move the form handling code above all HTML. As a side effect it will mitigate the notorious "Headers already sent" error.

Here's a more detailed guide than any of the other answers have mentioned: http://www.exchangecore.com/blog/how-redirect-using-php/

This guide includes reasons for using die() / exit() functions in your redirects, as well as when to use ob_flush() vs ob_start(), and some potential errors that the others answers have left out at this point.

Answer from Joe Meyer on Stack Overflow
Discussions

Redirect to PHP page after IF statement
Remove the echo line and put in header('Location: somepage.php') exit; More on reddit.com
๐ŸŒ r/PHPhelp
8
4
April 29, 2020
PHP contact form redirect to another page

Can you provide some code?

More on reddit.com
๐ŸŒ r/PHPhelp
11
4
December 7, 2016
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Go to a page via PHP - PHP - SitePoint Forums | Web Development & Design Community
December 27, 2011 - Hello, I was wondering if it was possible andI hoping someone could help me use PHP to go to a specific url / page. More specifically, as the suite inside an if statement. Here is a simple instance / example: if 3 > 5{ /* go to false.html / } else{ / go to true.html */ } Does a tone have an idea how I could do this or approach doing this?
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-make-a-redirect-in-php
How to make a redirect in PHP? - GeeksforGeeks
July 12, 2025 - Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) ...
๐ŸŒ
Hostinger
hostinger.com โ€บ home โ€บ tutorials โ€บ how to make a php redirect
How to make a PHP redirect
November 19, 2025 - Hereโ€™s a basic PHP redirect example of redirecting to another page within your same website (a relative URL): <?php // Redirects to contact-us.php in the same directory header("Location: contact-us.php"); exit; ?> The example below shows how to redirect to a completely different website (an absolute URL): <?php // Redirects to Google header("Location: https://www.google.com"); exit; ?>
๐ŸŒ
CCM
ccm.net โ€บ accueil โ€บ apps & sites โ€บ web
How to make redirect in PHP?
February 9, 2022 - <?php header('Location: /directory/mypage.php'); ?> If ever the target page is on another server, you include the full URL: ... According to HTTP protocol, HTTP headers must be sent before any type of content. This means that no characters should ever be sent before the header โ€” not even an empty space! For more information, read our article on how to send headers. ... By default, the type of redirection presented above is a temporary one. This means that search engines, such as Google, will not take the redirection into account when indexing.
Find elsewhere
๐ŸŒ
CCM
ccm.net โ€บ accueil โ€บ apps & sites โ€บ web
How to make redirect in PHP? - CCM NET
February 9, 2022 - <?php header('Location: /directory/mypage.php'); ?> If ever the target page is on another server, you include the full URL: ... According to HTTP protocol, HTTP headers must be sent before any type of content. This means that no characters should ever be sent before the header โ€” not even an empty space! For more information, read our article on how to send headers. ... By default, the type of redirection presented above is a temporary one. This means that search engines, such as Google, will not take the redirection into account when indexing.
๐ŸŒ
The Sitewizard
thesitewizard.com โ€บ php โ€บ how-to-redirect-to-another-web-page.shtml
How to Redirect to Another Web Page in PHP (thesitewizard.com)
July 30, 2019 - Once content is sent, the browser no longer expects any more headers, and anything sent to it will be regarded as part of a web page (or image or video or some other type of content). But by the time the PHP interpreter encounters the header() instruction in the above script, it is too late.
๐ŸŒ
nixCraft
cyberciti.biz โ€บ nixcraft โ€บ howto โ€บ apache โ€บ php redirect to another url / web page script example
PHP Redirect To Another URL / Web Page Script Example - nixCraft
March 8, 2015 - Warning: Cannot modify header information โ€“ headers already sent by (output started at H:\wamp\www\Pos-arif\admin\include\sidebar.php:21) in H:\wamp\www\Pos-arif\include\functions.php on line 19 ... im trying to find something that lets me put a hyperlink on my site and when it redirects it keeps them on my site and loads the other site under a nav bar type thing ... I have a registration form that sends an email to me when it is submitted. Right now it goes to my home page but I want it to go to my thanks page.
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ php โ€บ php scripts
How to Redirect With PHP | Envato Tuts+ - Code
May 21, 2020 - In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous ...
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ web servers โ€บ how to make a redirect in php
How to Make a Redirect in PHP (2 Methods) | phoenixNAP KB
December 16, 2025 - The fastest and most common way to redirect one URL to another is by using the PHP header() function. The general syntax for the header() function is as follows: ... $header. The URL or file name of the resource being redirected to.
๐ŸŒ
W3Docs
w3docs.com โ€บ php
How to Redirect a Web Page with PHP
Here, we will demonstrate how to use a helper function for redirecting a web page. Here is an example: <?php function Redirect($url, $permanent = false) { header('Location: ' . $url, true, $permanent ?
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to go from One Page to Another using PHP - YouTube
How to go from One Page to Another using PHP | PHP For BeginnersPHP Tutorial Playlist Link : https://www.youtube.com/playlist?list=PLm8sgxwSZoffaTr08FgSfiK9...
Published ย  August 8, 2024
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ redirect to php page after if statement
r/PHPhelp on Reddit: Redirect to PHP page after IF statement
April 29, 2020 -

Hello! I am trying to have the user be redirected to a PHP script after they have submitted the form. Currently, if someone submits the form I have it set up to display an echo message. I'm not sure what condition I should be using to redirect to this page. I'd love to have it be redirecting to the new page instead of echo 'You have successfully registered, you can login'; - Much appreciate the help!

if ($stmt = $con->prepare('INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)')) {

`// hash the password when someone submits`

`$password = password_hash($_POST['password'], PASSWORD_DEFAULT);`

`$stmt->bind_param('sss', $_POST['username'], $password, $_POST['email']);`

`$stmt->execute();`	

`echo 'You have successfully registered, you can now login!';`

} else {

`// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.`

`echo 'Could not prepare statement!';`

If there is a better way to do this to run in the background I'm more than happy to look at those too!

๐ŸŒ
Benjamin Crozat
benjamincrozat.com โ€บ home โ€บ blog โ€บ php redirect: how to send users to another page
PHP redirect: how to send users to another page
September 19, 2023 - PHPโ€™s header() manual notes that Location defaults to 302 unless you already set a 3xx status code, so choose the right one on purpose. ... That is the whole pattern. The only real decisions are: ... Use 302 when the target may change again. ... Use 301 when an old URL has moved for good.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ php โ€บ how do i make a redirect in php?
How do I make a redirect in PHP? | Sentry
The easiest way to add a redirect in PHP is to use the header() function. <?php $url = "https://example.com"; header('Location: '.$url); die(); ?> For example to redirect to https://example.com pass 'Location: https://example.com' into header.
๐ŸŒ
Medium
medium.com โ€บ @benjamincrozat โ€บ redirect-users-to-another-page-in-php-the-right-way-633ae8954166
Redirect users to another page in PHP, the right way | by Benjamin Crozat | Medium
October 7, 2022 - Redirect users to another page in PHP, the right way This article has been initially written for Benjamin Crozatโ€™s blog. To redirect users to another page in PHP, use the header() function. But if โ€ฆ