1

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.

Community
  • 1
  • 1
Josh K
  • 28,364
  • 20
  • 86
  • 132

2 Answers2

3

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

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
  • 33,305
  • 16
  • 69
  • 121
  • Right, I'm more interested in the name. Actually, only interested in the name. – Josh K May 01 '10 at 03:14
  • Updated answer with host name. – artlung May 01 '10 at 03:28
  • 1
    [Be careful](http://stackoverflow.com/questions/2297403/http-host-vs-server-name/2297421#2297421). – BalusC May 01 '10 at 03:36
  • 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 May 01 '10 at 03:37
  • Thus.. You need to configure the server :) See the link in my previous comment. – BalusC May 01 '10 at 03:38
  • What is the url you are calling? – artlung May 01 '10 at 03:40
  • 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 May 01 '10 at 03:40
  • @artlung: It's just the ip address of the server `http://111.111.111.111/phptest.php`. – Josh K May 01 '10 at 03:40
  • 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 May 01 '10 at 03:45
  • 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 May 01 '10 at 03:48