Home » Hosting » How to Redirect With PHP
June 22, 2021 by Kiet Duong

How to Redirect With PHP

php redirect

The client browser can be redirected to a specific URL using redirection. When switching domains, improving the structure of your site or switching to HTTPS, using redirection is a great tool to have. 

I’ll explain to you how to use PHP to redirect to a certain page in this article. I’ll show you how PHP redirects operate and what happens in the background.

What Is the Mechanism of Basic Redirection?

Let’s take a brief look at how HTTP redirection works before we get into the details of PHP redirection. Have a glance at the diagram below.
php redirect

Let’s know what is really going on in the image above:

  •         The client browser sends a request to the server for a particular page. The client has asked for the attributes of the index.php file in the case above.
  •         When the server gets a request for the index.php file, it needs to tell the client that it is no longer available or has been transferred and that it can now search for a new index.php. Including the 301 or 302 HTTP code, the server sends the Location header with a new URL. These are the redirection HTTP codes.
  •         When a browser sees a 301 or 302 code, it knows it has to make a new request to a new URL in order to get the content. In the above case, it makes a request to fetch the new index.php file.
  •         Finally, the contents of the latest URL are sent by a browser.

That’s how an HTTP redirection functions in its most simple form. We’ll go into how PHP redirection functions in the next segment.

How PHP Redirect Works?

When redirecting a user through one page to another in PHP, you can use the header() function. The header functionality allows users to send a raw HTTP location header, which executes the real redirection we discussed earlier.

Use Header Function

Let’s take a look at the header() function’s syntax.

header( $header, $replace, $http_response_code )

  •         $headerThe HTTP header string that you want to use. For redirection, we’ll use the Location header in our case.
  •         $replace: This is an additional parameter that specifies whether or not the header can replace a previous one with the same kind.
  •         $http response code: It helps you to submit a particular response code.

Let’s take a look at the following scenario and see if it works together.

<?php

// index.php

header(“Location: https://www.myownsite.com/new_index.php”);

exit();

?>

The client browser will be redirected to http://www.myownsite.com/new index.php after the above script is run. It sends a raw HTTP Location header with the 302-status code to the server in the background. The 302-status code is being used for short-term redirection, but the 301 code can be transmitted in the third argument for stable redirection, as seen in the example below.

<?php

// index.php

header(“Location: http://www.myownsite.com/new_index.php”, TRUE, 301);

exit();

?>

The 301 permanent redirect lets you inform search engines that a website is no longer usable and that it should be replaced.

After the header redirection, why do you use the Die() or Exit() functions?

Users with keen eyes would have found that in the preceding case, I used the exit() function. In fact, directly after the header redirection, you must use either the exit() or die() functions to stop script execution and prevent any unexpected effects.

As a result, after redirection, it’s still a good idea to use one of these functions.

The Well-Known Error: headers are already sent

If you’re a professional PHP programmer, you’ve almost certainly run into this well-known PHP error in your day-to-day PHP development. However, for beginners, encountering this mistake is particularly frustrating since it is extremely difficult to debug and repair. Much of the time, they have no idea that the header redirection is to blame. 

When using the header() function in your code, it’s a good rule of thumb to ensure you don’t submit any output before it. If you don’t, PHP will complain that “headers are already sent.” And if you’ve submitted a single white space before using the header function, this will happen.

We addressed one of the most critical facets of PHP programming in this article: redirection. We went through the fundamentals of HTTP redirection first, and then I showed you how it functions in PHP.  

433

Comments

Likes

Tags

Leave a Reply

Your email address will not be published.