Header ()

Source: Internet
Author: User

The header () function sends the original HTTP header to the client.

Definition and UsageHeader() function sends the original HTTP header to the client. It is important to recognize that it must be called before any actual output is sentHeader() function (in PHP 4and later versions, you can use output caching to resolve this issue):1PHP3//result Error4//output already exists before the header () is called5Header(' location:http://www.example.com/');6?>tips and comments from PHP4.4after that, the function prevents multiple headers from being sent at once. This is a protective measure against head-injection attacks. 01<?PHP02//Date in the past03Header("Expires:mon, 1997 05:00:00 GMT");04Header("Cache-control:no-cache");05Header("Pragma:no-cache");06?>07the user may set some options to change the browser's default cache settings. By sending the above header, you can override any of these settings to force the browser not to cache! Prompts the user to save a generated PDF file (Content-the disposition header is used to provide a recommended file name and force the browser to display the Save dialog box):01<?PHP02Header("Content-type:application/pdf");03//the file will be called Downloaded.pdf04Header("Content-disposition:attachment;filename= ' downloaded.pdf '");05//PDF Source in Original.pdf06ReadFile("Original.pdf");07?>08Microsoft IE5.5 There is a bug that prevents the above mechanism. by upgrading to ServicePack2or a higher version, you can resolve the bug. There are two kinds of specialHeaderCall. The first is the header with the string "http/"(not important for capitalization), which can be used to determine the HTTP status code to send. For example, if Apache is configured with PHP to handle error-handling requests that cannot find files (using the ErrorDocument Directive), you need to make sure that the script produces the correct status code. 1<?PHP2Header("http/1.0 404 Not Found")3?>The HTTP status code header line is always the first to be sent to the client, and does not control the actualHeader() Whether the call is the first one. Unless the HTTP header has been sent, it can be called at any time by using a new status lineHeader() function to overwrite the original. The second special case is the location of the: "header. It does not just send this hair back to the browser, it will also be a REDIRECT (302The status code is returned to the browser unless a 3XX status code has been previously issued. 1<?PHP2Header("location:http://www.example.com/");/*redirect browser*/3/*ensure that subsequent code is not executed after redirection*/4Exit;5?>HTTPThe/1.1 standard requires an absolute address URI as the location: parameter, but there are some clients that support relative URIs. You can usually use$_server[' Http_host '],$_server[' Php_self '] anddirname() function to generate an absolute URI from a relative URI:1<?PHP2Header("location:http://".$_server[' Http_host ']3.RTrim(dirname($_server[' php_self ']), '/\\ ')4. " /".$relative _url);5?>even if the session is enabled.The use_trans_sid,session ID will not be passed along with the location header information. Must be passed manually with SID constants. PHP scripts often produce dynamic content that must not be cached by the browser or proxy server. Many proxy servers and browsers can be disabled by the following methods:1<?PHP2Header("Cache-control:no-cache, must-revalidate");//http/1.13Header("Expires:mon, Jul 1997 05:00:00 GMT");//the past time4?>you may find that even if you do not output all of the above code, the page is not buffered. There are many options that users can set to change the browser's default caching behavior. By sending the above header, you should be able to overwrite any settings that could cause the script page to be cached. In addition, when the session is used, use theSession_cache_limiter() function and session.The Cache_limiter option can be used to automatically generate the correct cache-related headers. To rememberHeader() must be called before any actual output, whether from normal HTML tags, blank lines, or PHP. One common mistake is to passinclude(),require() or some other file access class function when reading code, there are some spaces or blank lines in the callHeader() was sent out before. Also in a separate php/This error is also common in HTML files. 1<?PHP2/*this will produce an error because something has been output before the header ()*/4Header(' location:http://www.example.com/');5?>from PHPFrom 4 onwards, some output buffering functions can be used to solve this problem. The price is to keep all the output from the browser on the server until the next command sends them. Can be used in codeOb_start() andOb_end_flush() to implement such a function, or by modifying PHP.INI in the output_buffering configuration option, or by modifying the server configuration file. If you want to prompt the user to save the sent data, such as a generated PDF file, you can send the Content-the disposition header provides the recommended filename to force the browser to pop up a Save File dialog box. View Sourceprint? 1<?PHP2//This will output a PDF file directly3Header(' Content-type:application/pdf ');4//doing so will prompt you to download the PDF file downloaded.pdf5Header(' Content-disposition:attachment; filename= "Downloaded.pdf");6//This is the source file for Original.pdf.7ReadFile(' Original.pdf ');8?>

Header ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.