How can I find the hostname of the web server?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-19-2005
Dave
 
Posts: n/a
Default How can I find the hostname of the web server?

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)

Reply With Quote
  #2 (permalink)  
Old 11-19-2005
Dave
 
Posts: n/a
Default Re: How can I find the hostname of the web server?

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)

Reply With Quote
  #3 (permalink)  
Old 11-19-2005
Disco Octopus
 
Posts: n/a
Default Re: How can I find the hostname of the web server?

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
Reply With Quote
  #4 (permalink)  
Old 11-19-2005
Dave
 
Posts: n/a
Default Re: How can I find the hostname of the web server?

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)

Reply With Quote
  #5 (permalink)  
Old 11-19-2005
Dave
 
Posts: n/a
Default Re: How can I find the hostname of the web server?

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)

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:20 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0