This is a discussion on How can I find the hostname of the web server? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I'm trying to find the hostname and IP address of the web server using php. I found this snippet ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm trying to find the hostname and IP address of the web server using php.
I found this snippet of code on the web: <?php $ip_address = $_SERVER['REMOTE_ADDR']; $host_name = $_SERVER['REMOTE_HOST']; echo "<p>ip address = $ip_address</p>"; echo "<p>hostname = $host_name</p>"; ?> but it returns for me. ip address = 192.168.0.10 hostname = so whilst it has resolved the IP address correctly, it has failed to find the hostname at all. The hostname is in /etc/inet/hosts and '/bin/hostname' returns it OK. DNS works fine too. I'm running php 5.0.2, apache 2.52 and Solaris 9 on a Sun. -- Dave K http://www.southminster-branch-line.org.uk/ Please note my email address changes periodically to avoid spam. It is always of the form: month-year@domain. Hitting reply will work for a couple of months only. Later set it manually. The month is always written in 3 letters (e.g. Jan, not January etc) |
|
|||
|
Dave wrote:
> I'm trying to find the hostname and IP address of the web server using php. > > I found this snippet of code on the web: > > <?php > $ip_address = $_SERVER['REMOTE_ADDR']; > $host_name = $_SERVER['REMOTE_HOST']; > > echo "<p>ip address = $ip_address</p>"; > echo "<p>hostname = $host_name</p>"; > ?> > > but it returns for me. > > ip address = 192.168.0.10 > > hostname = > > so whilst it has resolved the IP address correctly, it has failed to > find the hostname at all. Sorry, I correct myself. The 192.168.0.10 is the address of the broswer. This is clearly finding the client rather than the servers data. I want the hostname of the *server* -- Dave K http://www.southminster-branch-line.org.uk/ Please note my email address changes periodically to avoid spam. It is always of the form: month-year@domain. Hitting reply will work for a couple of months only. Later set it manually. The month is always written in 3 letters (e.g. Jan, not January etc) |
|
|||
|
Dave wrote:
> I'm trying to find the hostname and IP address of the web server using php. > > I found this snippet of code on the web: > > <?php > $ip_address = $_SERVER['REMOTE_ADDR']; > $host_name = $_SERVER['REMOTE_HOST']; > > echo "<p>ip address = $ip_address</p>"; > echo "<p>hostname = $host_name</p>"; > ?> > > but it returns for me. > > ip address = 192.168.0.10 > > hostname = > > so whilst it has resolved the IP address correctly, it has failed to > find the hostname at all. > > The hostname is in /etc/inet/hosts and '/bin/hostname' returns it OK. > DNS works fine too. > > I'm running php 5.0.2, apache 2.52 and Solaris 9 on a Sun. Is that what you really want? maybe you wnat $_SERVER['HTTP_HOST'] instead? -- a beef jerky site http://www.choicebeefjerky.com.au not a beef jerky site http://mycoolwheels.com/vote.cmks nobody ever dreams of working for the man |
|
|||
|
Disco Octopus wrote:
> Dave wrote: > > >>I'm trying to find the hostname and IP address of the web server using php. >> >>I found this snippet of code on the web: >> >><?php >>$ip_address = $_SERVER['REMOTE_ADDR']; >>$host_name = $_SERVER['REMOTE_HOST']; >> >>echo "<p>ip address = $ip_address</p>"; >>echo "<p>hostname = $host_name</p>"; >>?> >> >>but it returns for me. >> >>ip address = 192.168.0.10 >> >>hostname = >> >>so whilst it has resolved the IP address correctly, it has failed to >>find the hostname at all. >> >>The hostname is in /etc/inet/hosts and '/bin/hostname' returns it OK. >>DNS works fine too. >> >>I'm running php 5.0.2, apache 2.52 and Solaris 9 on a Sun. > > > Is that what you really want? maybe you wnat $_SERVER['HTTP_HOST'] > instead? > > You were quick !! No, that returns www.southminster-branch-line.org.uk, where southminster-branch-line.org.uk is the domain name. I want it to return "main-webserver" since that is the hostname of the machine. -- Dave K http://www.southminster-branch-line.org.uk/ Please note my email address changes periodically to avoid spam. It is always of the form: month-year@domain. Hitting reply will work for a couple of months only. Later set it manually. The month is always written in 3 letters (e.g. Jan, not January etc) |
|
|||
|
Dave wrote:
> I'm trying to find the hostname and IP address of the web server using php. I eventually found it myself - calling phpinfo() gave pointers to all of it. <?php $ip_address = $_SERVER["SERVER_ADDR"]; $server_port = $_SERVER['SERVER_PORT']; $host_name = $_ENV["HOST"]; echo "<p>ip address = $ip_address</p>"; echo "<p>server port = $server_port</p>"; echo "<p>hostname = $host_name</p>"; ?> Returns... ip address = 192.168.1.15 server port = 80 hostname = main-webserver -- Dave K http://www.southminster-branch-line.org.uk/ Please note my email address changes periodically to avoid spam. It is always of the form: month-year@domain. Hitting reply will work for a couple of months only. Later set it manually. The month is always written in 3 letters (e.g. Jan, not January etc) |