What is the use of header() function in php
❮ PHP Network Reference Show ExampleSend three HTTP headers to prevent page caching: // Date in the past ... ... Note: There are options that users may set to change the browser's default caching settings. By sending the headers above, you will override any of those settings and force the browser to not cache! Definition and UsageThe header() function sends a raw HTTP header to a client. It is important to notice that the header() function must be called before any actual output is sent! Syntaxheader(header, replace, http_response_code) Parameter Values
Technical Details
More ExamplesExampleLet the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box): header("Content-type:application/pdf"); // It will be called downloaded.pdf // The PDF source is in original.pdf ... ❮ PHP Network Reference View Discussion Improve Article Save Article View Discussion Improve Article Save Article The header() function is an inbuilt function in PHP which is used to send a raw HTTP header. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server, before any other output has been sent. The PHP header() function send a HTTP header to a client or browser in raw form. Before HTML, XML, JSON or other output has been sent to a browser or client, a raw data is sent with request (especially HTTP Request) made by the server as header information. HTTP header provide required information about the object sent in the message body more precisely about the request and response. Syntax: void header( $header, $replace = TRUE, $http_response_code ) Parameters: This function accepts three parameters as mentioned above and described below:
Return Values: This function doesn’t return any value. Example 1: Output: This will change location of header, i.e. redirect to the URL Example 2:
Hello World!
Output: Hello World! Array ( [0] => X-Powered-By: PHP/7.0.33 [1] => Expires: Sun, 25 Jul 1997 06:02:34 GMT [2] => Cache-Control: no-cache [3] => Pragma: no-cache ) The above example helps to prevent caching by sending header information which override browser setting to not-cache. Note: The header() functions is used multiple time in the example as one header is allowed to send at a time (since PHP 4.4) to prevent header injection attacks. Uses:
Reference: http://php.net/manual/en/function.header.php PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. What is the header () function in PHP?PHP | header() Function. The header() function is an inbuilt function in PHP which is used to send a raw HTTP header. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server, before any other output has been sent.
Where is header located in PHP?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.
What type of headers that PHP supports?The header() is a pre-defined network function of PHP, which sends a raw HTTP header to a client.
What is the syntax for including a header in PHP?The header() function sends a raw HTTP header to a client. It is important to notice that the header() function must be called before any actual output is sent!
|