The Header Location in PHP

The Header Location in PHP

  1. Introduction to the Header() Function and Its Syntax in PHP
  2. Use the header() Function With the location: Header String in PHP

This article will introduce the concept of the header() function and its syntax in PHP. It will cover the rules of writing header in PHP. This method will also work with the Content-Type and Content-Disposition header.

We will also introduce the location: header string in this article. We will explain the usage and essence of the location header in PHP. The article will demonstrate how the header location sends the response code and redirects the browser to another page.

Introduction to the Header() Function and Its Syntax in PHP

The header() function is an in-built PHP function that lets us send a raw HTTP header to the client. The header sent is in the raw form. We should invoke the header() function before any output is sent. The output in any form, like the output sent by the HTML tags or a PHP form, should be discarded before sending the header information. Thus we can control the information sent to the browser by the server before any output.

The syntax of the header() function is: header(string,replace,http_response_code);. The function accepts three parameters. The first argument is a header string. There are two types of header strings. The first type is a string that starts with HTTP/. It specifies the HTTP codes to be sent to the browser. The second type of the header string is the location: header, which redirects the browser to the specified location. The next parameter in the function is replace, which represents a boolean value. It is an optional parameter that determines if the header should replace the previous similar header. The third parameter, http_response code is also an optional parameter that forces the HTTP response code to the specified value.

For example, create a header() function and use the header string as Content-Type. Write the value of the Content-Type as application/pdf. Again create another header() function. This time, write the header string as Content-Disposition. Give the value of the string as attachment. Do not forget a semicolon after it. Write another attribute filename after the semicolon and provide the filename as download.pdf.

When we run the following script, then a download dialog box appears. It asks you to download a pdf file named download.pdf. The first header indicates the file should be of pdf format, and the second header denotes the filename of the file and forces the browser to display the dialog to save the file.

Example Code:

#php 7.x
<?php
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename="downloaded.pdf"');
?>

Use the header() Function With the location: Header String in PHP

We can use the header() function with the location: header string in PHP. The header string redirects the webpage to the location specified. It is generally used in web pages to redirect the user to a specific page after submitting the input. For instance, when the user inputs the correct credentials while logging in, we can use the header location to redirect them to the homepage. We can specify the boolean value and the response code in the header() function. However, these parameters are optional. The default boolean value is true that means that it will replace the previous similar header. We can also provide the response code as the third parameter. The default response code is 302. For example, we can write an array to a file and redirect the current page to another page displaying the message that the file has been written. We can use the file_put_contents() function to write into the file.

For example, create an array on the $day variable. Create the keys as weather and time and the respective values as Sunny and 1:30 pm. Then use the file_input_contents() and specify a file file.txt as the first parameter. Use the print_r() function as the second parameter. Supply the variable $day and boolean value true as the parameters of the print_r() function. Evaluate the whole expression using the if condition. Inside the if block use the header() function. Specify the location as message.php inside the function. Use the colon : to specify the location. Note that there should not be any gap between the location and the : colon. Create a PHP file message.php. Display a message in the file saying that the file has been written.

In the example above, the array is written to the file file.txt. The if condition evaluates true and the header() function redirects the location to message.php. Thus, the output is shown. We can also see the changed URL in the address bar. If there had been another header function below the existing header function, the latter one would replace the former header. This is because the default value of the replace option is true in the header() function.

Code Example:

#php 7.x
<?php
$day = array (
    'weather' => 'Sunny',
    'time' => '1:30 pm',
);
if(file_put_contents('file.txt', print_r($day, true))){
    header("location: message.php");
}
?>

Output:

The file has been written.
Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn