Relieable way to get a hosts name.

This is a discussion on Relieable way to get a hosts name. within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I'm trying to find the hostname (as would be reported by /bin/hostname) of a UNIX computer in a ...


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-20-2005
Dave
 
Posts: n/a
Default Relieable way to get a hosts name.

I'm trying to find the hostname (as would be reported by /bin/hostname)
of a UNIX computer in a bit of PHP.

This worked on 3 of my Sun machines (SPARCstation 20, Ultra 60 and Ultra
80).

$host_name = $_ENV["HOST"];

But after I patched Solaris 9 on the Ultra 60 (to make it current like
the other two machine), the HOST enviroment vaiable is not defined.

Running phpinfo(), I found that

$host_name = $_ENV["_INIT_UTS_NODENAME"];

will work on this patched machine, But _INIT_UTS_NODENAME does not work
on my other Sun boxes (despite the fact they are all patched from the
same patch cluster).

So now I have changed my code to try both environment variables, and
reject one if the length of the string returned is 0 bytes. Of course,
who is to say both are not going to be 0 bytes long.

Is there a reliable way of determining the hostname? I no *not* want the
domain name.

Running apache 2.0.52 on SPARC.
--
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-20-2005
Dave
 
Posts: n/a
Default Re: Relieable way to get a hosts name.

Dave wrote:

> Is there a reliable way of determining the hostname? I no *not* want the
> domain name.


That was supposed to say "I do not want the domain name".


--
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-20-2005
Philip Ronan
 
Posts: n/a
Default Re: Relieable way to get a hosts name.

"Dave" wrote:

> I'm trying to find the hostname (as would be reported by /bin/hostname)
> of a UNIX computer in a bit of PHP.
>
> This worked on 3 of my Sun machines (SPARCstation 20, Ultra 60 and Ultra
> 80).
>
> $host_name = $_ENV["HOST"];
>
> But after I patched Solaris 9 on the Ultra 60 (to make it current like
> the other two machine), the HOST enviroment vaiable is not defined.


On my virtually hosted account, phpinfo() doesn't reveal the host name at
all. However, this should work (assuming your host server does actually have
a name):

<?php
echo gethostbyaddr($_SERVER["SERVER_ADDR"]);
?>

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/

Reply With Quote
  #4 (permalink)  
Old 11-20-2005
Dave
 
Posts: n/a
Default Re: Relieable way to get a hosts name.

Philip Ronan wrote:
> "Dave" wrote:
>
>
>>I'm trying to find the hostname (as would be reported by /bin/hostname)
>>of a UNIX computer in a bit of PHP.
>>
>>This worked on 3 of my Sun machines (SPARCstation 20, Ultra 60 and Ultra
>>80).
>>
>>$host_name = $_ENV["HOST"];
>>
>>But after I patched Solaris 9 on the Ultra 60 (to make it current like
>>the other two machine), the HOST enviroment vaiable is not defined.

>
>
> On my virtually hosted account, phpinfo() doesn't reveal the host name at
> all. However, this should work (assuming your host server does actually have
> a name):
>
> <?php
> echo gethostbyaddr($_SERVER["SERVER_ADDR"]);
> ?>
>

Thanks, that did work on two servers I tried it on. I did not try the
3rd one is it is powered off at the minute and I can't be bothered to go
up into a cold garage to power it up. But I know it was in the system
data on php, so I guess it will work.

Thank you.

--
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-21-2005
Etienne Marais
 
Posts: n/a
Default Re: Relieable way to get a hosts name.

Philip Ronan wrote:

> "Dave" wrote:
>
>> I'm trying to find the hostname (as would be reported by /bin/hostname)
>> of a UNIX computer in a bit of PHP.
>>
>> This worked on 3 of my Sun machines (SPARCstation 20, Ultra 60 and Ultra
>> 80).
>>
>> $host_name = $_ENV["HOST"];
>>
>> But after I patched Solaris 9 on the Ultra 60 (to make it current like
>> the other two machine), the HOST enviroment vaiable is not defined.

>
> On my virtually hosted account, phpinfo() doesn't reveal the host name at
> all. However, this should work (assuming your host server does actually
> have a name):
>
> <?php
> echo gethostbyaddr($_SERVER["SERVER_ADDR"]);
> ?>
>


Another way may be

echo `hostname`; // For Unix-Ish Systems
but Philip's solution is probably preferable.

Etienne Marais
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 07:00 PM.


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