Header Location page jump reason for failure

Source: Internet
Author: User
Tags setcookie
HTTP header information
The function of the head information is many, the main one has the following several:
1. Jump
When the browser accepts the LOCATION:XXXX in the header message, it will automatically jump to the URL address that XXXX points to, which is a bit like writing a jump with JS. But this jump only browser know, regardless of the contents of the content, the user can not see.
Example: Header ("location:http://www.php.cn/");

2, the content of the specified page
The same XML file, if specified in the header information: Content-type:application/xml, the browser will parse it according to the XML file format. However, if the header message is: Content-type:text/xml, the browser will treat it as a text parsing. (The browser does not parse the file by extension)
Example: Header (' content-type:application/pdf ');

3. Accessories
Do not know if you have noticed, sometimes in some sites to download things, click to download the connection, the result browser will be the attachment as a Web page opened, the display is garbled, the problem is also related to the header information. Sometimes the browser according to Content-type to determine whether to open or save, so that sometimes you can judge the error (mainly the site designer forgot to write Content-type). In fact, there is another can be to specify the content as an attachment, need to save, this is: content-disposition:attachment; Filename= "xxxxx"
Example: Header (' content-disposition:attachment; filename= ' downloaded.pdf ');
Open the file and output
ReadFile (' original.pdf ');
HTTP protocol Status Code
1XX reserved
2XX indicates that the request was successfully received by
3XX request further refinement of requests for completion of customer requests
4XX Customer Error
5XX Server Error

Grammar:
Header (String,replace,http_response_code)
Parameters
String: Required. Specifies the string of headers to send.
Replace: Optional. Indicates whether the header replaces the previous header, or adds a second header. The default is true (replace). False (multiple headers of the same type are allowed).
Http_response_code: Optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and later)
Example 1:
REDIRECT Web pages

   <?php       Header ("location:http://www.php.cn/");      Exit;   ? >

Example 2:
Forces users to get the latest information each time they access the page, rather than using a client-side cache.

   <?php         //Tell the browser to expire on this page (in Greenwich Mean Time), as long as the date has passed.         header ("expires:mon,26 Jul 1970 05:00:00 GMT");         Tells the browser that the last update date of this page (in Greenwich Mean Time) is the same day, the purpose is to force the browser to get the latest data         header ("last-modified:". Gmdate ("D,d M Y h:i:s"). " GMT ");         Tell the client browser not to use the cache         header ("Cache-control:    no-cache,    must-revalidate");         Parameters (compatible with previous servers), which is compatible with the HTTP1.0 protocol         header ("Pragma:    no-cache");   >

Example 3:
Output status value to browser, primarily for access control

   <?php         Header (' http/1.1 401 Unauthorized ');         Header (' status:401 unauthorized ');     ? >

Example 4:
To restrict a user from being able to access the page, you can set the status to 404, as shown below, so that the browser is displayed as if the page does not exist

   <?php         Header (' http/1.1    404 Not    Found ');         Header ("Status:    404 Not    Found");//This error   ?>

Example 5:
Location of hidden files
HTML tags <a href= ' http://hostname/*.mp3 ' ></a> can be used for normal file download. If in order to keep confidential files, you can not send the file link to others, you may use the header function for file download.

Header ("Content-type:audio/mpeg"); header ("Content-disposition:attachment; Filename=filenale "); Header (" Content-description:php3 Generated Data ");

Example 6:
Enter content before the header function
In general, you cannot output HTML content before the header function, similar to the Setcookie () and session functions, which need to add message header information to the output stream. If there is an echo statement before the header () is executed, the "Warning:cannot Modify header Information-headers already sent by ..." error is reported when the header () is encountered later. This means that there cannot be any text, blank lines, carriage returns, etc. in front of these functions, and it is best to add the exit () function after the header () function. For example, in the following error notation, there is a blank line between the two PHP code snippets:

<?php//some code here?>//Here should be a blank line <?php    header ("http/1.1 403 Forbidden");    Exit ();? >

Cause: When the PHP script starts executing, it can send both the HTTP message header and the principal information. The HTTP message header (from the header () or the Setcookie () function) is not immediately sent, instead, It is saved to a list. This allows you to modify the header information, including the default title (for example, the Content-type title). However, once the script sends any non-header output (for example, using HTML or print () calls), then PHP must first send all headers and then terminate Httpheader. Then continue to send the principal data. From this point on, any attempt to add or modify header information is not allowed, and one of the above error messages is sent.
Workaround:
Modify php.ini Open Cache (output_buffering), output_buffering= 0 Modify to output_buffering=4096
or use the cache function Ob_start (), Ob_end_flush (), etc. in the program. The principle is: When output_buffering is enabled, PHP does not send httpheader when the script sends the output. Instead, it enters this output through a pipeline (pipe) into a dynamically increasing cache (only used in PHP4.0, which has a centralized output mechanism). You can still modify/add a header, or set a cookie because the header is not actually sent. When all the scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the contents of the output buffer.

Related Article

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.