Header Location REDIRECT statement failed to jump the page

Source: Internet
Author: User
Tags exit php code php script response code setcookie

function Header function:

Page Jump

HTTP header information
Header information has many functions, the most important of which are the following:
1, jump
When the browser accepts the location:xxxx in the header, it automatically jumps to the URL address that the XXXX points to, which is a bit like writing a jump with JS. But this jumps only the browser to know, no matter in the body content has the thing, the user cannot see.
Example: Header ("location:http://www.3lian.com/");

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

3, Accessories
Do not know if you have no attention, and sometimes in some sites to download things, the point of Download connection, the results of the browser as a Web page opened, inside the display is garbled, this problem is also related to the head information. Sometimes the browser is based on Content-type to determine whether to open or save, so sometimes you will judge the error (mainly the web designer forgot to write Content-type). In fact, there is another can 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 Retention
2XX indicates that the request was successfully received
3xx requesting customers to further refine requests for completion
4XX Customer Error
5XX Server Error

Header (String,replace,http_response_code)
String: Required. Specify the header string to send.
Replace: Optional. Indicates whether the header replaces the previous header, or adds a second header. The default is true (replace). False (allows multiple headers of the same type).
Http_response_code: Optional. Forces the HTTP response code to the specified value. (PHP 4 and later available)

Example 1:
REDIRECT Web pages

Header ("location:http://www.example.com/");


Example 2:
Force users to get the latest information each time they visit this page, instead of using a cached client.

Tells the browser when this page expires (in Greenwich Mean Time), as long as the date is already past.

Header ("expires:mon,26 June 1970 05:00:00 GMT");

Tell the browser that the last update date for this page (in GMT) is the same day, the purpose is to force the browser to get the latest information

Header ("last-modified:". Gmdate ("D,d M Y h:i:s"). " GMT ");

Tells the client that the browser does not use the cache

Header ("Cache-control:no-cache, must-revalidate");

Parameter (compatible with previous server), i.e. compatible HTTP1.0 protocol

Header ("Pragma:no-cache");

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

Header (' http/1.1 401 Unauthorized ');

Header (' status:401 unauthorized ');

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

Header (' http/1.1 404 Not Found ');

Header ("status:404 not Found"); This is a wrong notation.

Example 5:
Location of hidden files
The HTML tag <a href= ' Http://hostname/*.mp3 ' ></a> can be used to download normal files. If you want to keep a confidential file, you cannot tell the file link to others, you can use the header function to achieve file download.
Header ("Content-type:audio/mpeg");
Header ("content-disposition:attachment; Filename=filenale ");
Header ("Content-description:php3 generated Data");
Example 6:
Header function before entering content

In general, it is not possible to 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 header () executes, when the header () is encountered, the "Warning:cannot Modify header Information-headers already sent by ..." error is reported. This means that there can be no text, blank lines, carriage returns, etc. before 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 two PHP code snippets:

Some code here


This is supposed to be a blank line.


Header ("http/1.1 403 Forbidden");

Exit ();

When the PHP script starts executing, it can simultaneously send the HTTP message header (header) information and the principal information. The HTTP message header (from the header () or Setcookie () function) is not sent immediately, instead, it is saved to a list. This allows you to modify the header information, including the default headings (such as content-type headings). However, once the script sends any non-caption output (for example, using HTML or print () calls), then PHP must first send all headers and then terminate Httpheader. And then continue sending the principal data. From this point on, any attempt to add or modify header information is disallowed and sends one of the above error messages.

Modify PHP.ini to open cache (output_buffering) and modify output_buffering=0 to output_buffering=4096
Or use the caching 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 output. Instead, it enters the output through the pipeline (pipe) into the dynamically incremented cache (which can only be used in PHP4.0, which has a centralized output mechanism). You can still modify/add headers, or set cookies, because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser and then sends the contents of the output buffer.

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.