Redirection allows you to redirect the client browser to a different URL. You can use it when you're switching domains, changing how your site is structured, or switching to HTTPS.
In this article, I'll show you how to redirect to another page with PHP. I'll explain exactly how PHP redirects work and show you what happens behind the scenes.
Learn PHP With a Free Online Course
If you want to learn PHP, check out our free online course on PHP fundamentals!
-
FREEPHPPHP Fundamentals
How Does the Basic Redirection Work?
Before we dive into the specifics of PHP redirection, let’s quickly understand how exactly HTTP redirection works. Take a look at the following screenshot.
Let’s understand what’s going on in the above screenshot:
- The client browser requests a specific page from the server. In the above example, the client has requested the contents of the index.php file.
- The server receives the index.php file request and wants to inform the client that it’s no longer available or moved somewhere else, and it should look to a new file instead: new_index.php. The server sends the
Location
header with a new URL along with the 301 or 302 HTTP code. These are the HTTP codes for redirection. - When a client browser encounters the 301 or 302 code, it knows that it has to initiate an another request to a new URL to fetch the content. It initiates a request to fetch the
new_index.php
file in the above example. - Finally, a server sends the contents of the new URL.
So that’s how a basic HTTP redirection works. In the next section, we’ll discuss how the PHP redirection works.
How Redirection Works in PHP?
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 section.
How to Use Header Function
Let's go through the syntax of the header()
function.
header( $header, $replace, $http_response_code )
$header
: This is the HTTP header string which you want to use. In our case, we’ll use theLocation
header for redirection.$replace
: It’s an optional parameter which indicates whether the header should replace a previous similar header.$http_response_code
: It allows you to send a specific response code.
Now, let’s have a look at the following example to understand how it works altogether.
<?php // index.php header("Location: https://www.yoursite.com/new_index.php"); exit(); ?>
When the above script is executed, it’ll redirect the client browser to http://www.yoursite.com/new_index.php. In the background, it sends a raw HTTP Location
header along with the 302 status code. The 302 status code is used for temporary redirection, but if you want permanent redirection, you can pass the 301 code in the third argument as shown in the following snippet.
<?php // index.php header("Location: http://www.yoursite.com/new_index.php", TRUE, 301); exit(); ?>
The 301 permanent redirect allows you to inform to the search bots that the page is no longer available, and it can be replaced with a new page.
Why Should You Use Die()
or Exit()
Function After the Header Redirection?
Users with sharp eyes would have noticed that I’ve used the exit()
function in the above example. In fact, it’s mandatory that you use either exit()
or die()
function immediately after the header redirection to stop script execution and avoid any undesired results.
So it’s always recommended practice to use one of these functions after redirection.
The Famous Error: Headers are Already Sent
If you’re an experienced PHP programmer, I’m sure you’ve come across this famous PHP error at some point of time in your day-to-day PHP development. Although for beginners, the situation is really annoying when they encounter this error, since it’s really hard to debug and fix. In most of the cases, they don’t even have a clue that it’s caused by the header redirection.
The rule of thumb is that when you use the header()
function in your script, you need to make sure that you don’t send any output before it. Otherwise, PHP will complain with the headers are already sent error. Even if you've sent a single white space before using the header function.
Conclusion
In this post, we discussed one of the important features of PHP programming: redirection. First, we went through the basics of HTTP redirection and then I demonstrated how it works in PHP.
The Best PHP Scripts on CodeCanyon
Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon. With a low-cost one time payment, you can purchase these high-quality WordPress themes and improve your website experience for you and your visitors.
Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.
-
PHP12 Best PHP Event Calendar and Booking Scripts
-
PHP10 Best PHP URL Shortener Scripts
-
PHP12 Best Contact Form PHP Scripts for 2020
-
PHPComparing the 5 Best PHP Form Builders
-
PHPCreate Beautiful Forms With PHP Form Builder
No comments:
Post a Comment