Java, J2EE & SOA Certification Training
- 33k Enrolled Learners
- Live Class
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.
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
<?php header("Location: https://www.edureka.co/"); ?>
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.
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.
<html> <body> <form action="script.php" method="post"> E-mail: <input type="text" name="email"> <input type="submit" name="submit" value="submit"> </form> </body> </html>
Let us continue with this Header location in PHP
<?php $email=$_POST['email']; $file="fetchedemail.php"; file_put_contents($file,$email . PHP_EOL, FILE_APPEND); header("location: ./thank.php"); ?>
<?php echo("Thank you for submitting your mail"); ?>
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.
<pre> <?php header("Expires: Sun, 22 Jun 1997 04:00:00 GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); print_r(headers_list()); ?> </pre>
 => X-Powered-By: PHP/7.0.33
 => Expires: Sun, 22 Jun 1997 04:00:00 GMT
 => Cache-Control: no-cache, must-revalidate
 => 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.