Re: server name that the user agent used
| From: | Tom Worster | Date: | Mon, 14 Sep 2009 19:36:05 +0000 |
| Subject: | Re: server name that the user agent used | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298030@lists.php.net to get a copy of this message | ||
On 9/14/09 2:11 PM, "Lars Torben Wilson" <torben@php.net> wrote:
> Tom Worster wrote:
>> On 9/13/09 10:24 PM, "Tommy Pham" <tommyhp2@yahoo.com> wrote:
>>
>>> --- On Sun, 9/13/09, Tom Worster <fsb@thefsb.org> wrote:
>>>
>>>> From: Tom Worster <fsb@thefsb.org>
>>>> Subject: [PHP] server name that the user agent used
>>>> To: "PHP General List" <php-general@lists.php.net>
>>>> Date: Sunday, September 13, 2009, 8:21 PM
>>>> when using apache with one vhost that
>>>> responds to a few different hostnames,
>>>> e.g. domain.org, y.domain.org, x.domain.org, let's say the
>>>> vhost's server
>>>> name is y.domain.org and the other two are aliases, is
>>>> there a way in php to
>>>> know which of these was used by the user agent to address
>>>> the server?
>>>>
>>> Did you see what comes up with php_info() for
>>> $_SERVER["SERVER_NAME"] or $_SERVER["HTTP_HOST"] ?
>>
>> SERVER_NAME returns whatever apache has as the vhost's configured server
>> name.
>>
>> the php manual says of HTTP_HOST: "Contents of the Host: header from the
>> current request, if there is one." in which the last 4 words are a little
>> off-putting. but:
>>
>>
>> http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
>>
>> i much more encouraging. the field is mandatory and should have what i'm
>> looking for. it's absence is cause for a 400. casual testing (with a modern
>> non-ie browser) seems to bear this out.
>>
>> so i'll try using that with fallback to my current techniques if i don't
>> find a good value in HTTP_HOST.
>
> The reason that it might not be available is that PHP is not always
> running in a web context. $_SERVER['HOST_NAME'] would have no meaning,
> for instance, in the CLI SAPI.
>
> However, if running under a web SAPI, and if the web server provides the
> info, PHP will pass it on to its scripts.
and, for the record, in the web environment there's cause for caution using
either HTTP_HOST or, if UseCanonicalName is off or you can't be sure of its
value, SERVER_NAME:
http://shiflett.org/blog/2006/mar/server-name-versus-http-host
in my current app, i should be able to match for acceptable values and fall
back to defaults on failure.