php

What is Header Location In PHP?

PHP redirection is a powerful feature that allows developers to redirect the client’s browser to a different HTTP. It is a server-side process that quickly redirects the user from one web page to another. The redirects are beneficial when switching domains or implementing HTTPS. There are several methods available to perform redirection in PHP, but the header method is one of the easiest and safest of all.

In this tutorial, we will discuss the header location in PHP in detail, explaining how it works and how to use it effectively for your website.

What is Header Location In PHP?

In PHP, header location refers to the process of redirecting a user to another web page or URL by using the header() function. A web server responds to a request for a web page by sending the header and body of the requested page. The header usually contains response metadata, such as the HTTP status code and any headers that affect caching compression, or other aspects of the response.

You can send a specific header directing the browser to redirect to a new URL using PHP’s header function, practically changing the location of the current web page. This is an effective method for changing domains, implementing HTTPS, or simply redirecting users to a new page.

Syntax for Header Location Function in PHP

The basic syntax for the Header Location function in PHP is as follows:

header(string $header, bool $replace = true, int $http_response_code = 0): void

 

In the above syntax:

  • The header specifies the filename or URL of the resource to which the user wants to redirect.
  • The replace determines whether to replace a previous header or not; its default value is true, but setting it to false allows using the same headers multiple times.
  • http-response-code sets the HTTP response code to a specific value; the default value is 302, but users can customize it as needed.

Note: The header is important as it refers to the header location set by the header() function. While replace and http-response-code are additional parameters that users can select based on their choice. These parameters allow you to control how the redirect is handled and what HTTP response code is sent in the response to the client.

The below example illustrates the working of the header function in PHP:

<?php
header("Location: http://www.example.com/another-page.php");
exit();
?>

 

In the above code, the header() function is used to set the Location header to the URL of the new page that we want to redirect the user to. After setting the header, we use the exit statement to stop the current PHP script and ensure that the redirect is executed immediately.

Note: Instead of http://www.example.com/another-page.php, you can use any valid URL that you want to redirect the user to. This can be an absolute or native URL, depending on your needs. Also, make sure to use a valid URL and that the user has authorized access to visit the entered URL. Further, remember that the header() function needs to be called before delivering any output to the browser.

Status Code in Location Header

When using the Location header in PHP to perform a redirect, the HTTP response code sent to the client is 302, indicating a temporary redirect. The temporary redirect instructs the client’s browser to request the new URL using a new HTTP GET request. To indicate that the redirect is permanent, use a different status code.

A permanent redirect can be indicated by the HTTP response code 301. This status code informs the client that the requested resource has been relocated permanently and that future requests should use the new URL:

<?php
header("Location: http://www.example.com/another-page.php", true, 301);
exit();
?>

 

Conclusion

PHP’s header() function is a handy feature that redirects users to another web page or URL. It is a server-side operation that can be used for a variety of tasks, including changing domains and implementing HTTPS. The syntax of the header location function is basic and easy to use, and developers can customize it to meet their own requirements.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.