What is the difference between HTTP_HOST and SERVER_NAME in PHP?

前端 未结 10 2433
青春惊慌失措
青春惊慌失措 2020-11-22 07:07

What is the difference between HTTP_HOST and SERVER_NAME in PHP?

where:

  • HTTP_HOST === $_SERVER[\'HTTP_HOST\'
10条回答
  •  故里飘歌
    2020-11-22 07:59

    As I mentioned in this answer, if the server runs on a port other than 80 (as might be common on a development/intranet machine) then HTTP_HOST contains the port, while SERVER_NAME does not.

    $_SERVER['HTTP_HOST'] == 'localhost:8080'
    $_SERVER['SERVER_NAME'] == 'localhost'
    

    (At least that's what I've noticed in Apache port-based virtualhosts)

    Note that HTTP_HOST does not contain :443 when running on HTTPS (unless you're running on a non-standard port, which I haven't tested).

    As others have noted, the two also differ when using IPv6:

    $_SERVER['HTTP_HOST'] == '[::1]'
    $_SERVER['SERVER_NAME'] == '::1'
    

提交回复
热议问题