PHP file with HTTP Header

Source: Internet
Author: User
Tags echo command
The PHP code that executes on the server is typically used to respond to HTTP requests from the client. One of the simplest PHP files is this:

 
  

According to the HTTP standard, an HTTP message is generally divided into two parts: the HTTP header (header) and the HTTP body (body), and the header message must be output before the body message. In the PHP file, the header function can be used to output header information, and the echo command can output the body message, if you do not use the header function, before the output body will output the default header information. Therefore, for the following example:

 
 
  

Since there is a blank line at the front of the php file, PHP will output it as the body of the text, which will warn you that the header message has already been output after executing the headers function. In addition, some files use Unicode encoding, which can also cause this problem if you do not remove the BOM characters from the beginning of the file. There is no problem with blank lines on the last side of the php file, because PHP automatically removes a newline character after?>.

By the way, take a look at some common HTTP codes, which are listed below:

$ OK 201 Created-Bad Request 401 unauthorized 403 Forbidden 404 Not Found 405 Method not allowed 415 unsupported Med IA Type Internal Server Error

Resources:
[1] PHP Manual: First PHP page
[2] PHP Manual: Header
[3] PHP manual: Echo
[4] Hypertext Transfer Protocol--http/1.1
[5] HTTP Header detailed
[6] Modify HTTP Headers (Examples)
[7] PHP closed tag output extra empty exercise XML page display error
[8] HTTP status code-Wikipedia

  • 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.