What is the use of header function in php and its usage ?

The header() functions are used to send the raw HTTP header to the client which means it sets the headers for the HTTP response.

You an perform different sorts of jobs by using header function. Such as changing the page location , set cache control , set the timezone and so on ..

The General syntax for header function is :

header(string,replace,http_response_code);

 For example : 

header() used as set page location i.e redirect to the page location

<?php 
   header('Location: http://www.domin.com/page.php');
?>

header() used as sending the HTTP status.

<?php
     header("HTTP/1.0 404 Not Found");
?> 

header() used for content types i.e generating pdf files 

<?php 
    header('Content-Disposition: attachment; filename="file.pdf"');
?>

header() used as Caching directives

<?php
 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  header("Expires: Sat, 28 Jul 1998 05:00:00 GMT"); // Date in the past..
?> 

Leave a Reply

Your email address will not be published.Required fields are marked *