Redirection is one of the most popular demands in many of the Web Applications. In PHP, you can easily redirect from one webpage to another with using the inbuilt header function. It is also sometimes called a PHP Redirect function because of its popular use in Redirecting to other webpages.
Let’s first understand a bit about the PHP Header function. This function is basically used to send a RAW HTTP Header to client-side. In any use case, headers have to be sent before printing any kind of output on the webpage. Therefore, you should use the PHP Header function before producing any kind of output with your PHP Code.
PHP Header Function Syntax
PHP header function has three parameters but only the first parameter is required for the basic purpose of redirection. The three parameters in order are defined below.
header(header, replace, HTTP_response_code)
- header. It is a String Parameter used to define the Header String to sent to the client. It is also the one and only required parameter.
- replace. It is a boolean type and an optional parameter. The default value is True. It specifies whether to replace the existing header os same name or not. If set True, it will replace any existing duplicate header or if set false, it will not. Therefore, the setting if is False, may result in duplicate headers sent to the client.
- HTTP_response_code. This is also optional and sets the response code of the HTTP Request. You can specify different response codes like 301 (For Permanent Redirection), 302, 307 (For Temporary Redirection), etc.
How to Code a PHP Redirect?
Coding a PHP redirect with header() function is pretty easy. Whenever we have to redirect a request to some other URL, Location header has to be sent to the client. You can send the Location header with the value of the URL, where the request has to be redirected. The example of a PHP Redirect is given below.
https://url-to-redirect.com/ is the URL where you wish to redirect.
It is also recommended to use
exit after the header function. Most of the times if your PHP Redirect using header() function is not working, it will work, if you’ll put
exit immediately after the header() function statement.
The use of header() function PHP Redirect is not limited to just URLs, but you can also use it for redirect files of any type or extension.
301, 302 & 307 Redirects
Some of the most common types of Redirects are 301, 302 and 307. 301 Redirect is used to redirect to a permanently moved page. 302 is a redirect to temporarily available page of similar content and 307 Redirect is a temporary redirection.
You just have to specify the Redirect Code as the second parameter to the header() function. Example of 301 Redirect is given below.
header("Location: https://url-to-go.com/", 301);
More uses of PHP Header Function
As there are a lot of different headers that can be sent to a client, therefore, there are also multiple and different types of scenarios various header() function can be used, apart from just redirection purposes.
For example, it can be used to set Content-Type or prompting the user to save a file on their computer.