AJAX request differentiation $ _ SERVER [''http _ X_REQUESTED_WITH ''] Small Solution

Source: Internet
Author: User

Due to the influence of many template codes, we thought php had such a custom variable: $ _ SERVER ['HTTP _ X_REQUESTED_WITH ']. In fact, this is not the case at all.
$ _ SERVER is an array containing header information, path, and script locations.

Some header information exists in the system and cannot be changed, such as HOST and USER_AGENT. Note that we often use $ _ SERVER ['HTTP _ host'], $ _ SERVER ['HTTP _ USER_AGENT '], which has an HTTP prefix. In this case, there is no HTTP prefix in the header information, and the information name is only HOST or the like, $ _ SERVER automatically adds an HTTP prefix to them. 1 is to prevent conflicts with other information, and 2 is to indicate that they are header information. therefore, all the variables in $ _ SERVER with an HTTP prefix are header information, but none are available, for example, $ _ SERVER ['query _ string']. $ _ SERVER ['php _ SELF '] and so on.

You can create custom header information and send the information to the SERVER. The SERVER records the header information and stores them in the $ _ SERVER variable, of course, the variable name is changed to "HTTP _" connected to the user's variable name. for example, the above ajax request is judged. in fact, before sending an asynchronous request to the server, add a header to the header. The actual statement should be:

HttpRequest object. setRequestHeader ("X_REQUESTED_WITH", "arbitrary string"); whether the SERVER exists through $ _ SERVER ['HTTP _ X_REQUESTED_WITH, or a specific value to determine whether it is an ajax request or which ajax request is required. therefore, this not only verifies whether the request is an ajax request, but also obtains more information.

We can also set any other information in the header for data exchange with the server.

For example, xmlHttpRequest. setRequestHeader ("test", "just a test"); then the value of the variable $ _ SERVER ['HTTP _ test'] on the requested SERVER Page is "just a TEST "; note that the test in front of it is not case sensitive. $ _ SERVER ['HTTP _ test'] must be capitalized.

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.