views:

72

answers:

3

Going off of my other question and its pair.

I'm looking to grab the Hostname:Port value as found in the phpinfo() function. I can't seem to find it anywhere and it's not in $_SERVER.

+2  A: 

Apache 2.0 server with PHP 5.2 $_SERVER['SERVER_PORT'] should give you 80 for http connections.

For the hostname, for me these two work identically:

echo $_SERVER['HTTP_HOST'];
echo $_SERVER['SERVER_NAME'];

...should do the trick.

Read more about the $_SERVER variable here

artlung
Right, I'm more interested in the name. Actually, only interested in the name.
Josh K
Updated answer with host name.
artlung
[Be careful](http://stackoverflow.com/questions/2297403/http-host-vs-server-name/2297421#2297421).
BalusC
It's not in `$_SERVER`. At all. `print_r($_SERVER);` turns up nothing, especially not the host name as listed in the `phpinfo();` cell.
Josh K
Thus.. You need to configure the server :) See the link in my previous comment.
BalusC
What is the url you are calling?
artlung
Ah, that was a similar answer I was getting elsewhere, I just didn't want to have to set that up for every VirtualHost, and then it will probably screw with some other stuff I'm doing. Eh.
Josh K
@artlung: It's just the ip address of the server `http://111.111.111.111/phptest.php`.
Josh K
Okay, so for the url: http://111.111.111.111/phptest.php -- the `$_REQUEST` has to do with the **actual request** being made, and because the hostname is nowhere to be found, the IP address (111.111.111.111) is what would be expected. Because the same website may be aliased and accessible from multiple ways, there is not necessarily any way for you to get a canonical "name" for the server. You might have the same site come up under http://localhost/ http://111.111.111.111/ http://example.com http://www.example.com/ http://www.example.org/ - so which is right? All of them?
artlung
One thing you could do is force the domain, so let's say I wanted my site to come up for all the values in previous comment, but I wanted to force it to be something in particular http://stackoverflow.com/questions/1495165/iirf-url-rewrite-to-force-www-prefix-example-com-www-example-com would be an approach.
artlung
A: 

Have you tried apache_request_headers()?

deadkarma
Nope, nothing there.
Josh K
+2  A: 

You can use the $_SERVER['SERVER_NAME'] for this. You only need to configure the server accordingly that it returns the expected value. You're apparently using newer than Apache HTTPD 1.3.

You need to set UseCanonicalName directive to on in the <VirtualHost> entry in httpd.conf (also check the warning at the bottom of the linked document!).

<VirtualHost *>
    ServerName example.com
    UseCanonicalName on
</VirtualHost> 

Also see this answer.

BalusC
Okay, that works. Now funny thing, instead of going to `/var/www` like it was before I restarted Apache it's looking for `/htdocs`. I don't think I changed anything, but why is it doing that?
Josh K
Basically the `DocumentRoot` changed but it's not changed anywhere I checked.
Josh K
Redeclare the `DocumentRoot` in the `<VirtualHost>`.
BalusC
Yeah, figured that out. Actually posted it on SF.
Josh K