HTTP_HOST 和 SERVER_NAME 的區別

Corwien發表於2019-02-16

PHP的SERVER保留變數中 HTTP_HOSTSERVER_NAME 經常是一樣的,原因是伺服器埠是80,那麼HTTP_HOST將省略不顯示,而HTTP服務的預設埠就是80,所以大多數情況下,HTTP_HOST都沒有顯示這個埠號。而如果使用的是ssl連結,那麼埠號將被顯示出來。

所以HTTP_HOST = SERVER_NAME:SERVER_PORT

訪問請求:
12.62.201.12:8013/test.php

列印$_SERVER

print_r($_SERVER);
Array
(
    [HTTP_HOST] => 12.62.201.12:8013
    [HTTP_CONNECTION] => keep-alive
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    [HTTP_REFERER] => http://58.62.201.107:8091/
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.8
    [HTTP_COOKIE] => PHPSESSID=4v3njmj1bot72q66kpo3mcam42
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/php/bin:/usr/local/apache/bin:/root/bin
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache/2.2.31 (Unix) PHP/5.5.36
    [SERVER_NAME] => 12.62.201.12
    [SERVER_ADDR] => 192.168.17.120
    [SERVER_PORT] => 8013
    [REMOTE_ADDR] => 2.2.2.2
    [DOCUMENT_ROOT] => /usr/local/apache/htdocs
    [SERVER_ADMIN] => you@example.com
    [SCRIPT_FILENAME] => /usr/local/apache/htdocs/test.php
    [REMOTE_PORT] => 2104
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => 
    [REQUEST_URI] => /test.php
    [SCRIPT_NAME] => /test.php
    [PHP_SELF] => /test.php
    [REQUEST_TIME_FLOAT] => 1501652634.963
    [REQUEST_TIME] => 1501652634
)