Interview Questions for PHP header

  1. What are header() in PHP?

         PHP header() function is used to send a raw HTTP header to a client. PHP header() must be called before any actual output is sent either by normal HTML tags, blank lines in a file, or from PHP.

        Syntax :
        header(header, replace, http_response_code) ;
        Parameter,
        header : It is required. It specifies the header string to send.
        replace: It is optional. It indicates whether the header should replace previous or add a second header.
        http_response_code : It is optional. It forces the HTTP response code to the specified value such as 200, 404, 303 etc.
 

    2. How to use header() for downloading file in php?

    Below header functions are used to download file from server.
    header("Content-type:application/pdf"); => It defines content type of file.
    header("Content-Disposition:attachment;filename='destination.pdf'"); = > Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box.
    readfile("source.pdf");  = > Read it original file.

 3.How to disable caching option for dynamic pages in php?

   We can disable cache for dynamic pages using below php header function.
   header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
   header("Expires: Mon, 12 Mar 1956 08:00:00 GMT"); // Specify past date
   Your pages aren't cached after setting past date in Expires.

4. How to redirect page using php?

   Below header() function is used to redirect page.
   header("Location: http://www.aryatechno.com"); // pass page link to Location parameter.
 

5. How to send HTTP status code using php?

 Below header() function is used to send HTTP status code like 200, 404, 301 etc.
header("HTTP/1.0 404 Not Found");


       

Comments

Leave a Reply

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

17949