Redirecting users and traffic to certain areas of your web application and external resources are common tasks in PHP development. Luckily redirecting to another location in PHP is straightforward with the header function. Using the header function allows you under certain situations direct a user to a particular place, maybe after they enter their username and password on a logic form, you may wish to direct them to a dashboard. In this guide, we will explore how to redirect a user to another location within your own website as well as to an external resource. Before we dive into how this works with PHP, let's understand what HTTP headers are.
What are HTTP headers?
The PHP header function allows web developers to set HTTP headers when a user requests certain pages or resources from your website. Headers make up an important part of the web browsing processing and allow the browser and web server to send additional information along with the request body. For example, when browsing a web page, your browser will send a header to the web server with your expected browser language such as the below example. Here this header says that it accepts the English British language (en-GB) which can be used by the server to return the correct language, suitable to the user's browser preference.
Accept-Language: en-GB,en
A web server might send you back the following, it tells the browser what the page encoding is (UTF-8) and the type (text HTML).
content-type: text/html; charset=UTF-8
Having communication between your browser and web server ensures things load correctly in the right format and helps aid security, such as enforcing HTTPS connections. Because PHP is a server-side language and requires a web server to run, it means that it too can set an array of HTTP headers that will be sent back to the user. Let's explore how they work in the context of PHP.
What is the PHP header function?
The header function in PHP allows you to set these types of raw headers (like the ones above) on the return of the user's HTTP requests. The header function accepts a string header, a bool if it should replace a previously set header and response code. Some examples of this might be a 301, 302 or 404. This function does not return anything after being called and therefore the return type is void.
header(string $header, bool $replace = true, int $response_code = 0): void
How to redirect to another location using PHP
The best way to redirect in PHP is by using the header function, we can direct a user to another location on your website or to an external. It's important to remember we don't want to output anything else to the browser when we use a header like this, and we should terminate the PHP script immediately afterward. This is because PHP will trigger a warning if the headers are already set when attempting to send additional output. This includes HTML and blank lines in a file. In order to redirect to another location, use the 'Location' header (with a capital "L") followed by a colon and then the page location. The example below sets a header telling the browser that the new location is "/php", followed by terminating the rest of the script.
header('Location: /php');
die();
If for some reason the redirect isn't working, check that no other output occurs on the page by terminating the rest of the script after the header function has been called and that you include the 'Location' header before your page path.
How to use PHP and redirect to another page
When redirecting it is also possible to direct a user to another page with PHP, and it is also possible to redirect to a page with query string parameters and anchor tag added too. Let's break down each option to find out how we can do this with PHP. In example 1, we're redirecting a user to the about-us.php page using the header function. In example 2 we're doing the same, but this time we have appended the page with a query string "lang=en". You'll notice that it starts with a question mark. The web server uses this as an indication that after the question mark are query strings, following the key=value pattern. And finally in example 3, we're anchor tagging the ID "contactform" with the page. When this is present, the browser will scroll the user down to this location. A question mark isn't needed in example 3 and instead, the hash symbol is used to indicate this is an anchor tag.
# Example 1
header('Location: about-us.php');
die();
# Example 2
header('Location: about-us.php?lang=en');
die();
# Example 3
header('Location: about-us.php#contactform');
die();
How to do a 301 redirect in PHP
Performing a 301 HTTP redirect in PHP is easy with the header function because it allows us to enter an HTTP response code (of type integer), and this is the third parameter that's passed. Looking at the example below, we're using this function to do a 301 redirect to '/php'. This is a great way to control the relevant HTTP response code when doing a page redirect in PHP.
header('Location: /php', false, 301);
die();
Redirect to index.php page in PHP
One of the most common redirects (including in web frameworks) is directing to index.php or in a web application the router file. To do this you can again use the header function provided by PHP. The example below shows how you can redirect a user to the index.php of your website application.
# Redirect a user to index.php
header('Location: index.php');
die();
How to redirect to an external location using PHP
Using a similar code as the above, we can set the location to be an external URL too. Passing the full URL path works in the same way as redirecting to a page locally on your own website. It can be the root website or a subpage of the external website, it's completely up to you. You can also add query strings to the location as well if you require it.
header('Location: https://www.example.com');
die();
Conclusion
Using PHP's header function is a great way to redirect users from one page to another, from submitting a form to logging someone in and out. If you're using a CMS or web framework, such as WordPress or Laravel, it's generally better practice to use their native implementations of the header function in PHP.
- Don't output anything after the header function has been called
- Use the third parameter to set the HTTP response code of type integer
- You can redirect to another page on your website or an external page
- Add query string params to redirect URLs similar to how they appear in a URL bar
- It's also possible to include anchors in the redirect path