4

I basically need to create a anchor that will navigate to a different port on the server. For instance, our server is hosted on domain.com:555, we need a link to navigate to domain.com:777. The catch is that it's not always domain.com. We can expect the DNS to malfunction, in which case we will use ip address to navigate like xx.xx.xx.xx:555 and xx.xx.xx.xx:777.

I need get the hostname of the server where the PHP script is running. I tried using SERVER_ADDR, but that for reason gives me the private IP of the server.

So how do I get the domain / ip part of the url?

HyderA
  • 20,651
  • 42
  • 112
  • 180

2 Answers2

14

You want:

$_SERVER['HTTP_HOST']

From the $_SERVER predefined variable, which, for 'HTTP_HOST' contains:

Contents of the Host: header from the current request, if there is one.

You might also find:

$_SERVER['SERVER_NAME']

useful, and this Stack Overflow discussion of their relative merits instructive.

Community
  • 1
  • 1
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • Which? `HTTP_HOST` or `SERVER_NAME`? The answer to both is probably no - go read that discussion I linked to! – Dominic Rodger Aug 16 '10 at 11:03
  • @gAMBOOKa - It is always reliable provided your server has not outputted the host name in the header, which is highly unlikely if it is a web hosting server. – SimonDowdles Aug 16 '10 at 11:03
  • @gAMBOOKa I think the bottom line is, it's as reliable as it gets. If you want to be 1000% sure, use a hard coded setting in your app. – Pekka Aug 16 '10 at 11:12
  • @webfac: the host header is REQUIRED if you're on a name-based virtual host. As such HTTP_HOST will always have the name the site was accessed by. SERVER_NAME is what the webserver's been configured to call itself, which is not necessarily the site it's serving. – Marc B Aug 16 '10 at 15:10
3

Try $_SERVER['HTTP_HOST']; this will give you the host name / domain name

SimonDowdles
  • 2,026
  • 4
  • 24
  • 36
  • 3
    @Dominic Rodger - Apologies for double posting your answer, I only saw your response after I posted, the credit is yours! – SimonDowdles Aug 16 '10 at 11:02