PHP and MySQL (56 Blogs) Become a Certified Professional

What Is Header Location In PHP?

Last updated on Nov 25,2020 89.3K Views

We can automatically move one of the visitors to our website from one page to another in PHP. Normally when we hear about redirection, we hear about using it with a programming language called JavaScript but it is also very useful in PHP. The main reason, it is useful for PHP is to redirect users once they have entered the information as input into our web application. This article will focus on Header Location in PHP

So let us get started with this Location Header In PHP

Location Header In PHP

What is header()?

header() function is an inbuilt function that is used to send a raw HTTP header to a client in raw form. Basically, HTTP functions allow you to manipulate information sent to the browser by the webserver before any other output has been sent.  It must be called before sending any actual output, either by normal HTML tags, blank lines in a file or from a PHP file.

Syntax: header(string,replace,http_response_code);

string: It consists of a header string. Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory.

replace: It is optional which indicates whether the header should add a second header or replace previous.

http_response_code: It is also optional which forces the HTTP response code to a specified value(available in PHP 4.3 and higher).

This is the basic example change location of the header, i.e. redirect to URL


output - Header Location In PHP - Edureka

With file_put_contents(), we create an HTML form. When somebody puts information into an HTML form, they are sent to a script. Then the script takes the information and puts it into a file. Let’s say you are trying to gather some basic information, you can create a form that will send the information to a script and the script will then write that information to a file but the problem is that once we hit submit we are going to move to the script which is going to write the information to a file. we are going to be stuck there unless there is some way to redirect to another webpage. So if we don’t use the redirection we submit the data and we will be getting a blank page.

Moving on with this Header Location in PHP article,

Data Fetching and Redirection

I have created a simple form that takes your email address when we run the below script file “email.php” then it sends that information to a script that will do two things first it will write the data to a file “fetchedemail.php” and then it will redirect to “thank.php” web document.


<form action="script.php" method="post">
E-mail: <input type="text" name="email">
<input type="submit"&nbsp; name="submit" value="submit">

Let us continue with this Header location in PHP


file_put_contents($file,$email . PHP_EOL, FILE_APPEND);
header("location: ./thank.php");

thank.php: Header Location in PHP

echo("Thank you for submitting your mail");

output - Header Location In PHP - Edureka

output - Header Location In PHP - Edureka

output - Header Location In PHP - Edureka

Scripts of PHP also generate dynamic content that must not be cached by any proxy caches or client browser between the client browser and server.

Below is the final bit of  this Header location in PHP article. The below example helps to prevent caching by sending header information which overrides browser setting to not-cache.

header("Expires: Sun, 22 Jun 1997 04:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");




    [0] => X-Powered-By: PHP/7.0.33

    [1] => Expires: Sun, 22 Jun 1997 04:00:00 GMT

    [2] => Cache-Control: no-cache, must-revalidate

    [3] => Pragma: no-cache


With this we come to an end of this article on Header Location In PHP, If you found this article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  Array Merge In PHP  articleand I will get back to you.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What Is Header Location In PHP?