Undefined index HTTP_HOST even though it is checked

前端 未结 5 2273
栀梦
栀梦 2021-02-18 13:17

Here\'s the code:

if (isset($_SERVER[\'HTTP_HOST\']) === TRUE) {
  $host = $_SERVER[\'HTTP_HOST\'];
}

How is it possible to get an \"Undefined

5条回答
  •  逝去的感伤
    2021-02-18 14:18

    When a request is done with an empty host:

    GET / HTTP/1.1
    Host:
    

    Then isset($_SERVER['HTTP_HOST']) is true!

    It is better to use empty like:

    $host = '';
    if (!empty($_SERVER['HTTP_HOST'])) {
      $host = $_SERVER['HTTP_HOST'];
    }
    
    

    For detailed info take a look here https://shiflett.org/blog/2006/server-name-versus-http-host

提交回复
热议问题