Web Page to Browser header('Location: http://www.somesite.com/page.php'); in your scripts to redirect the browser to a different page. The part inside the parentheses is actually an HTTP header request header. The PHP header function can be used in the same manner with any http header. The URL specified in the location header above must be an absolute URL. It is a good idea to follow the header function with exit(); in a PHP script, to ensure that the code does not continue to execute after the redirect. PHP programmers sometimes find that the location header or some other header function fails. The likely reason for this is that it is preceded by browser output. The request header must always be the very first thing that is sent to the browser. The PHP header function sends the header to the browser in the same order as it occurs in the PHP code. Therefore, if the PHP code outputs anything to the browser, even something as minor as a blank line or a cookie, before the code gets to the line containing the header function, the header written by the header function will fail because it wasn't part of the header's first output to the browser. Fortunately for PHP programmers, there are the ob_start() and ob_end_flush() functions which cause the output to the browser to be cached until all of that output for the entire page is assembled, eliminating this problem. The "ob" stands for "output buffering." ob_start() goes before the beginning of any browser output, and ob_end_flush() goes at the end of the output. For example: <?php As you can see, this would not have worked without the output buffering functions because output would have already been sent to the browser before the header() function. Alternatively, the output_buffering configuration directive can be set in the php.ini or server configuration files. The Refresh: http header can also be used with the PHP header() function, to redirect the user after a time delay. This example provides a three second delay: header('Refresh: 3; url= http://www.somesite.com/pagetwo.php '); Another very common use of http headers is to prevent browsers from caching the page. This code snippet can be used to prevent it: <?php header('Pragma: no-cache'); // HTTP/1.0 (old browsers) Still another use is to force a "Save File" dialog box with a recommended file name, for downloading a file, using the Content-Disposition: header as in this example: <?php Between Web Pages Another area of HTTP header usage is communication between web pages. Request headers are often used for this purpose. At the beginning of this article, you saw the GET method used. Another one you may be familiar with is POST. There are several others, but those two are probably the most useful in web development. The biggest use is "under the hood" in form submissions, which as you know, utilize the GET or POST methods. Conclusion In this article, we barely scratched the surface of any aspect of HTTP headers and their usage. For more in depth information on this useful subject, I refer you to the W3C Standard RFC 2616 and to an excellent article in Wikipedia on the subject that contains links to further resources.
blog comments powered by Disqus |
|
|
|
|
|
|
|