[single-choice] that function can check if the HTTP header has been sent or sent to where?

Source: Internet
Author: User
Tags response code

  1. Headers_sent ()
  2. Http_response_code ()
  3. sent ()
  4. Header ()
Definition and usage

The Headers_sent () function checks whether the HTTP header has been sent and where it was sent.

Returns True if the header was sent, otherwise false.

Grammar
Headers_sent (file,line)
Parameters Description
file, Line

Optional.

If you set the file and line parameters, Headers_sent () stores the PHP source file name and row number at the beginning of the output in the file and lines variables.

Hints and Notes

Note: Once the header block has been sent, you cannot use the header () function to send other headers. Use this function to at least avoid error messages related to HTTP headers.

Note: The optional file and line parameters are newly added in PHP 4.3.

Example Example 1
<?php//if the header is not sent, send an if (! headers_sent() )  {  header ("location:http://www.w3school.com.cn/");  Exit;  }? >
Example 2

Use the optional file and line parameters:

<?php//Pass $file and $line for later use//do not assign them pre-values if (! headers_sent($file, $line) )  {  header ("location:http://www.w3school.com.cn/");  Exit;  Trigger an error here  }else  {  echo ' Headers sent in $file on line $line ";  Exit;  }? >

The meaning of the HTTP protocol status code is mainly divided into five categories, which are generally:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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

---------------------------------------------------------------

Continue the initial request has been accepted and the customer should continue to send the remainder of the request. (HTTP 1.1 new)

101 Switching Protocols Server translates the client's request to another protocol (HTTP 1.1 new)

Successful
=================================

All OK, the answer document for Get and post requests is followed.

The 201 Created Server has created the document, and the location header gives its URL.

202 Accepted has accepted the request, but the processing has not been completed.

203 Non-authoritative Information The document has returned normally, but some of the answer headers may be incorrect because a copy of the document (HTTP 1.1 new) is being used.

204 No Content There is no new document, the browser should continue to display the original document. This status code is useful if the user refreshes the page on a regular basis and the servlet can determine that the user's document is new enough.

205 Reset content is not new, but the browser should reset what it displays. Used to force the browser to clear the form input (HTTP 1.1 new).

The 206 Partial Content client sends a GET request with a range header that the server has completed (HTTP 1.1 new).

Redirection
==================================

Multiple Choices customer requested documents can be found in multiple locations that have been listed in the returned document. If the server wants to make a preference, it should be indicated in the location answer header.

301 Moved Permanently The document requested by the customer elsewhere, the new URL is given in the location header, and the browser should automatically access the new URL.

302 Found is similar to 301, but the new URL should be treated as a temporary replacement, not a permanent. Note that the corresponding status information in HTTP1.0 is "Moved temporatily".
When the status code appears, the browser can automatically access the new URL, so it is a useful status code.

Note that this status code can sometimes be used with 301 substitutions. For example, if the browser mistakenly requests Http://host/~user (the trailing slash is missing), some servers return 301, and some return 302.

Strictly speaking, we can only assume that the browser will automatically redirect only if the original request is get. See 307.

303 See other is similar to 301/302, except that if the original request is the Post,location header the specified redirect target document should be fetched via get (HTTP 1.1 new).

304 Not Modified client has buffered documents and issued a conditional request (typically providing a if-modified-since header indicating that the customer only wants to update the document than the specified date). The server tells the customer that the original buffered document can continue to be used.

305 Use proxy The document requested by the client should be extracted from the agent server indicated by the location header (HTTP 1.1 is new).

307 Temporary Redirect and 302 (Found) are the same. Many browsers incorrectly respond to a 302 response for redirection, even if the original request is post, even though it can actually be redirected only if the answer to the POST request is 303. For this reason, HTTP 1.1 has been added in 307 to allow for more cleanup of the region in several status codes: When a 303 response occurs, the browser can follow the redirected get and post requests, and if the 307 answer, the browser can only follow the redirect to the GET request. (HTTP 1.1 new)

Client Error
=====================

A syntax error has occurred in the request requests.

401 Unauthorized customer attempts to access password-protected pages without authorization. A www-authenticate header is included in the answer, and the browser displays the user name/Password dialog box, and then makes a request again after filling in the appropriate authorization header.

403 Forbidden resource is not available. The server understands the customer's request, but refuses to process it. This is usually caused by the permissions set on the file or directory on the server.

404 Not Found Could not find the resource at the specified location. This is also a common answer.

The 405 method not allowed request methods (GET, POST, HEAD, Delete, PUT, trace, etc.) do not apply to the specified resource. (HTTP 1.1 new)

406 not acceptable the specified resource has been found, but its MIME type is incompatible with the client specified in the Accpet header. (HTTP 1.1 new)

407 Proxy authentication Required is similar to 401, which means that the client must be authorized by the proxy server first. (HTTP 1.1 new)

408 Request Timeout The customer has not made any requests during the waiting time of the server license. Customers can repeat the same request at a later time. (HTTP 1.1 new)

409 Conflict is usually associated with a put request. The request cannot succeed because the request conflicts with the current state of the resource. (HTTP 1.1 new)

410 Gone The requested document is no longer available, and the server does not know which address to redirect to. It differs from 404 in that returning 407 means that the document has permanently left the specified location, and 404 indicates that the document is unavailable for unknown reasons. (HTTP 1.1 new)

411 Length Required The server cannot process the request unless the client sends a content-length header. (HTTP 1.1 new)

412 Precondition Failed Some of the prerequisites specified in the request header failed. (HTTP 1.1 new)

413 Request Entity Too Large the size of the target document exceeds the size that the server is currently willing to handle. If the server thinks it can process the request later, it should provide a retry-after header. (HTTP 1.1 new)

414 Request Uri Too long URI. (HTTP 1.1 new)

416 requested range not satisfiable server does not meet the Range header specified by the customer in the request. (HTTP 1.1 new)

Server Error
===================================

The Internal server Error server encountered unexpected conditions and was unable to complete the customer's request.

The 501 not implemented server does not support the functionality required to implement the request. For example, a customer sends a put request that is not supported by the server.

502 Bad Gateway server as a gateway or proxy, the server returned an illegal reply in order to complete the request to access the next server.

503 Service Unavailable Server failed to answer due to maintenance or heavy load. For example, a servlet might return 503 if the database connection pool is full. A retry-after header can be supplied when the server returns 503.

504 Gateway Timeout is used by a server acting as a proxy or gateway, indicating that it is not able to get answers from remote servers in a timely manner. (HTTP 1.1 new)

505 HTTP version not supported server does not support HTTP versions as specified in the request. (HTTP 1.1 new)

PHP header () function

PHP HTTP functions

Definition and usage

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

It is important to recognize that the header () function must be called before any actual output is sent (in PHP 4 and later, you can use output caching to resolve this issue):

header(‘Location: http://www.example.com/‘); ?>
Grammar
Header (String,replace,http_response_code)
Parameters Description
String Necessary. 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)
Hints and Notes

Note: After PHP 4.4, this function prevents multiple headers from being sent at once. This is a protective measure against head-injection attacks.

Example Example 1
<?php//Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Cache-Control: no-cache");header("Pragma: no-cache"); ? >

Note: The 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!

Example 2

Prompts the user to save a generated PDF file (the content-disposition header is used to provide a recommended file name and to force the browser to display the Save dialog box):

<?php header("Content-type:application/pdf"); //file will be called Downloaded.pdf header("Content-Disposition:attachment;filename=‘downloaded.pdf‘"); //pdf Source in Original.pdf ReadFile ("original.pdf");? >

Note: Microsoft IE 5.5 has a bug that prevents the above mechanism. You can resolve the bug by upgrading to a version of Service Pack 2 or higher.

PHP HTTP functions

Correct answer:

[single-choice] that function can check if the HTTP header has been sent or sent to where?

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.