Recently, in the development of station group software, problems related to determining sub-station points based on access domain names were used. PHP has two variables for obtaining the current domain name: HTTP_HOST and SERVER_NAME. I want to know the difference between the two and which is more reliable. Recently, I used the website group software to determine the subsite Based on the access domain name. PHP has two variables for obtaining the current domain name: HTTP_HOST and SERVER_NAME. I want to know the difference between the two and which one is more reliable.
Script ec (2); script
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: |
|
|
ServerName 111cn.net
ServerAlias www.111cn.net
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.