For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2005 > Relieable way to get a hosts name.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Relieable way to get a hosts name.
Dave

2005-11-20, 6:56 pm

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)

Dave

2005-11-20, 6:56 pm

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)

Philip Ronan

2005-11-20, 6:56 pm

"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/

Dave

2005-11-20, 6:56 pm

Philip Ronan wrote:
> "Dave" wrote:
>
>
>
>
> 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)

Etienne Marais

2005-11-21, 3:56 am

Philip Ronan wrote:

> "Dave" wrote:
>
>
> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com