Failed to jump to the page using the headerlocation redirection statement _ PHP Tutorial

Source: Internet
Author: User
The headerlocation redirection statement fails to redirect the page. Function header: page jump http header information has many functions, mainly including the following: 1. jump when the browser accepts Location: xxxx in the header information, function header:

Page jump

Http header information
Header information has many functions, mainly including the following:
1. jump
When the browser accepts Location: xxxx in the header information, it will automatically jump to the URL address pointed to by xxxx, which is similar to writing a jump using js. But this jump is only known by the browser, no matter whether there is anything in the body, the user can not see it.
Example: header ("Location: http://www.bkjia.com /");

2. specify the webpage content
If Content-type: application/XML is specified in the header of an xml file, the browser parses the file according to the XML file format. However, if the header is Content-type: text/xml, the browser will regard it as saved text parsing. (The browser does not parse the file according to the extension)
Example: header ('content-type: application/pdf ');

3. attachment
I don't know if you have noticed that some websites sometimes download things. after clicking download link, the browser opens the attachment as a webpage, and all the garbled characters are displayed, this issue is also related to header information. Sometimes the browser determines whether to open or save the Content based on the Content-type, and sometimes determines the error (mainly because the website designer forgets to write the Content-type ). In fact, you can specify the Content as an attachment and need to save it. this is: Content-Disposition: attachment; filename = "xxxxx"
Example: header ('content-Disposition: attachment; filename = "downloaded.pdf "');
// Open the file and output
Readfile('original ');

HTTP status code
1 × reserved
2×× indicates that the request is successfully received.
3×× further refine the request to complete the request
4×× customer error
5×× server error

Syntax:
Header (string, replace, http_response_code)
Parameters
String: required. Specifies the header string to be sent.
Replace: Optional. Indicates whether the header replaces the previous header or adds the second header. The default value 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. (PHP 4 and later versions are available)

Example 1:
Redirect webpage

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

Exit;

?>
Example 2:
Force users to obtain the latest information each time they access this page, instead of using the cache that exists on the client.

// Tell the browser the expiration time of this page (expressed by Greenwich Mean Time), as long as it is a date that has passed.

Header ("Expires: Mon, 26 Jul 1970 05:00:00 GMT ");

// Tell the browser the last update date (expressed by Greenwich Mean Time) of this page, that is, the day, to force the browser to obtain the latest information

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 ");

// Parameter (compatible with the previous Server), that is, compatible with HTTP1.0 protocol

Header ("Pragma: no-cache ");

?>
Example 3:
Outputs the status value to the browser, which is mainly used for access permission control.

Header ('http/1.1 401 unauthorized ');

Header ('status: 401 unauthorized ');

?>
Example 4:
To restrict a user from accessing the page, you can set the status to 404, as shown below, so that the browser shows that the page does not exist.

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

Header ("status: 404 Not Found"); // This write method is incorrect

?>
Example 5:
Hide file location
Html tags can be used to download common files. To keep files confidential, you can use the header function to download files.
Header ("Content-type: audio/mpeg ");
Header ("Content-Disposition: attachment; filename = filenale ");
Header ("Content-Description: PHP3 Generated Data ");
Example 6:
Input content before the header function

Generally, html content cannot be output before the header function. for example, there are setcookie () and session functions. these functions need to add message header information to the output stream. If an echo statement exists before the header () is executed, when header () is encountered later, "Warning: Cannot modify header information-headers already sent .... "error. That is to say, there cannot be any text, blank lines, and carriage return before these functions, and it is best to add the exit () function after the header () function. For example, the following error code contains an empty line between two php code segments:

// Some code here

?>

// It should be a blank line

Header ("http/1.1 403 Forbidden ");

Exit ();

?>
Cause:
When the PHP script starts execution, it can send both the http message header (title) information and the subject information. the http message header (from header () or SetCookie () function) is not sent immediately. Instead, it is saved to a list. this allows you to modify the title information, including the default title (for example, Content-Type title ). however, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate HTTPheader. then send the subject data. at this time, any attempt to add or modify the Header information is not allowed, and one of the above error messages will be sent.

Solution:
Modify php. ini to open the cache (output_buffering) and change output_buffering = 0 to output_buffering = 4096.
Alternatively, use the cache functions ob_start () and ob_end_flush () in the program. Principle: when output_buffering is enabled, PHP does not send HTTPheader when the script sends the output. Instead, it inputs the output to the dynamically added cache through the pipeline (pipe) (which can only be used in PHP4.0 and has a centralized output mechanism ). You can still modify/add the header or set the cookie 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 content in the output buffer.

There are many functions of redirecting the http header information on the webpage, mainly including the following: 1. when the browser receives the Location: xxxx in the header information, it will...

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.