header() function
It is a built-in PHP function to send the raw HTTP header to the client. The syntax of this function is shown below.
Syntax:
header( $header, [$replace, [$http_response_code]] )
This function can take three arguments. The first argument is mandatory, and the last two arguments are optional. The $header is used to store the header string that contains the location of the redirection. The $replace defines whether to replace the previous similar header, and the value of this argument is Boolean. The $http_response_code is used to store a specific response code that will send to the user.
Example-1: Redirect URL with default status code
Create a PHP file with the following code that will redirect to the new location after waiting for 2 seconds. Here, the die() function is used to terminate the script. When the header() function is used with one argument, then 302 is used as the default HTTP code.
Output:
After executing the code, The URL is redirected to the location http://localhost/php/contactForm/index.html after 2 seconds. If you inspect the code and open the Network tab, then it will show 302 as the default status code.
Example-2: Redirect URL permanently
Create a PHP file with the following code that will redirect to the new location after waiting for 2 seconds. Here, the die() function is used to terminate the script. Here, the header() function is used with three arguments. The TRUE is used for the second argument and 301 is used for the third argument. The 301 status code is used to redirect permanently.
Output:
After executing the code, The URL is redirected to the location http://localhost/php/contactForm/index.html after 2 seconds. If you inspect the code and open the Network tab, then it will show 301 as a status code that indicates the URL is moved permanently.
Example-3: Redirect URL temporary
Create a PHP file with the following code that will redirect to the new location after waiting for 2 seconds. Here, the die() function is used to terminate the script. Here, the header() function is used with three arguments. The TRUE is used for the second argument and 307 is used for the third argument. The 307 status code is used to redirect temporarily.
Output:
After executing the code, The URL is redirected to the location http://localhost/php/contactForm/index.html after 2 seconds. If you inspect the code and open the Network tab, then it will show 307 as a status code that indicates the URL is redirected temporarily.
Example-4: Redirect URL based on the condition
Create a PHP file with the following code that will redirect the URL based on the conditional statement. An HTML form is designed in the script to redirect URL based on the selected value of the drop-down list. Here, the drop-down list contains three values. When Google is selected from the drop-down list then the PHP script will redirect the URL to the location https://google.com with the default status code, 302. When LinuxHint is selected from the drop-down list then the PHP script will redirect the URL to the location https://linuxhint.com with the status code 301. When Fahmidasclassroom is selected from the drop-down list, then the PHP script will redirect the URL to the location, https://fahmidasclassroom.com with the status code, 302.
<head>
<title>Header Example</title>
</head>
<body>
<form method="post" action=#>
<select name="web">
<option>Google</option>
<option>LinuxHint</option>
<option>FahmidasClassroom</option>
</select>
<input type="submit" name="submit" value="Go" />
</html>
<?php
//Check the submit button is pressed or not
if(isset($_POST["submit"]))
{
if($_POST['web'] == 'Google')
{
//Redirect to the particular location
header("Location: https://google.com");
}
elseif($_POST['web'] == 'LinuxHint')
{
//Redirect to the particular location
header("Location: https://linuxhint.com",TRUE,301);
}
else
{
//Redirect to the particular location
header("Location: https://fahmidasclassroom.com");
}
die();
}
?>
Output:
After executing the code, the following output will appear in the browser that will display a drop-down list with three values and a Go button. The status code is 200 now. After redirection, the status code will be changed.
If Google will select from the drop-down, then it will redirect to the location https://google.com after pressing the Go button, and the following image will appear. The default status code, 302, is generated here.
If the LinuxHint selects from the drop-down, then it will redirect to the location https://linuxhint.com after pressing the Go button, and the following image will appear. The permanent status code, 301, is generated here.
Conclusion:
The different uses of the PHP header() function are explained in this tutorial by using multiple examples. The redirection can be done temporarily and permanently based on the status code used in the header() function. This tutorial will help the readers know more about the purpose of redirection and apply it by using PHP script in their web application when required.