The difference between server_name and http_host in PHP $_server

Source: Internet
Author: User

On this issue, search engine found a large article, there are many problems, in this find a correct saying: http://mimiz.cn/index.php/php/php-http_host-server_name-difference/

Reprint a bit

Reference

Recently in the development Station group software, the use of access to the domain name to determine the sub-site related issues. PHP gets the current domain name there are two variables http_host and server_name, I want to know the difference between the two and which is more reliable.

First of all, I would like to say that Baidu on those revolving articles are nonsense!
Have said the same, have said different, did not explain the reason, not verified on each other, wasting the audience time.

Let's say I have personally verified + Check official documents + official Bug list + official mailing list + SitePoint + WebmasterWorld + Google Summary:

Same point:
The same information is output when the following three conditions are met.
1. Server is 80 port
2. ServerName is set correctly in Apache Conf
3. http/1.1 Protocol Specification

Different points:
1. The usual situation:
_server["Http_host"] under the http/1.1 protocol specification, the information is output based on the HTTP request of the client.
_server["SERVER_NAME"] by default, the ServerName value in the Apache configuration file httpd.conf is output directly.

2. When the server is not 80 ports:
_server["Http_host"] outputs the port number, for example: mimiz.cn:8080
_server["SERVER_NAME"] will output servername value directly
So in this case, it can be understood as: Http_host = Server_name:server_port

3. When servername in profile httpd.conf is inconsistent with the domain name requested by http/1.0:
The httpd.conf configuration is as follows:
<virtualhost *>
ServerName mimiz.cn
Serveralias www.mimiz.cn
</virtualhost>
Client Access domain name www.mimiz.cn
_server["Http_host"] Output www.mimiz.cn
_server["SERVER_NAME"] Output mimiz.cn

Therefore, in the actual procedure, should try to use _server["Http_host", more insurance and reliable.

The difference between server_name and http_host in PHP $_server

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.