Differences between PHP $ _ SERVER ["SERVER_NAME"] and $ _ SERVER ['http _ host ']

Source: Internet
Author: User
Tags http request

First, I would like to say that the articles on Baidu are all nonsense!
There are some similarities and differences, but they do not explain the reasons. If they are not verified, they will be transferred to each other, wasting the audience time.

The following is a summary of my personal verification + official documents + official BUG list + official email list + sitepoint + webmasterworld + google:

Similarities:

When the following three conditions are met, the two will output the same information.
1. The server is port 80.
2. Set ServerName correctly in apache conf.
3. HTTP/1.1 Protocol specification

Differences:

1. General situation:

_ SERVER ["HTTP_HOST"] under the HTTP/1.1 protocol specification, information is output according to the client's HTTP request.
_ SERVER ["SERVER_NAME"] by default, the ServerName value in the configuration file httpd. conf of apache is directly output.

2. When the server is not port 80:

_ SERVER ["HTTP_HOST"] outputs the port number, for example, 111cn.net: 8080.
_ SERVER ["SERVER_NAME"] directly outputs the ServerName value
Therefore, it can be understood as: HTTP_HOST = SERVER_NAME: SERVER_PORT

3. When the ServerName in the configuration file httpd. conf is inconsistent with the domain name of the HTTP/1.0 request:

Httpd. conf is configured as follows:

The code is as follows: Copy code

<Virtualhost *>
ServerName 111cn.net
ServerAlias www.111cn.net
</Virtualhost>
Client Access domain name www.111cn.net
_ SERVER ["HTTP_HOST"] outputs www.111cn.net
_ SERVER ["SERVER_NAME"] outputs 111cn.net

Therefore, in actual programs, _ SERVER ["HTTP_HOST"] should be used as much as possible, which is relatively safe and reliable.

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.