
|
View Full Version : Finding server hostname with php
JonBlower 07-15-2005, 05:29 PM | Hi.
Im wondering if it is possible to find out the hostname of the server that the file is being hosted on with php.
For example, i have a personal domain, www.jonnebob.co.uk.
I have it running on a dedicated server with the hostname jon1.jonnebob.co.uk. Say i get a lots of hits and need a second server for whatever reason. This new server becomes jon2.jonnebob.co.uk.
Basically my script, it will be on both servers, but i want it to be able to tell if it is on jon1 or jon2. Is this possible?
Cheers.
Jon |
adaml 07-15-2005, 05:44 PM You could set a small variable in your script that specifies what the hostname is, as allowing to show the Hostname of a server could be a security risk.
E.g.
$hostname = "jon2.jonnebob.co.uk";
Hope that helps! |
JonBlower 07-15-2005, 05:48 PM | Well thats how i do it now, but im trying to reduce the amount of script editing i have to do with every new server i add.
I was really just wondering if its possible, if not, i can stick to my current method......no harm if i have too! |
fastduke 07-15-2005, 05:56 PM $_SERVER["SERVER_NAME"]
as for the security aspect? What?
do you have a PTR record for jonnebob.co.uk? |
opera.mp3 07-15-2005, 06:01 PM if you run:
<?php print '<pre>'.print_r($_SERVER, true).'</pre>'; ?>
you will see $_SERVER['HTTP_HOST'] or $_SERVER['SERVER_NAME'] may fulfill your needs.
refer to the documentation:
http://us3.php.net/manual/en/reserved.variables.php#reserved.variables.server
edit: I was too slow on the typing and looking for reference link.. |
JonBlower 07-15-2005, 06:28 PM Opera, I did refer to the documentation before. $_SERVER['SERVER_NAME'] just shows me my domain, www.jonnebob.co.uk not my server hostname, jon1.jonnebob.co.uk
Fastduke, i think so. |
opera.mp3 07-15-2005, 06:34 PM ahh, sorry, I actually misread your post.
maybe try doing something like this:
$hostname = `echo \$HOSTNAME`; |
JonBlower 07-15-2005, 06:36 PM Nevermind. Ive sorted my problem now. I've basically found out what i wanted to do isnt possible. Cheers for your help all. |
fastduke 07-15-2005, 06:52 PM well jon1.jonnebob.co.uk does not exist in dns(not that this has to do anything with finding the hostname from the server itself). and your jonnebob.co.uk resolves but the reverse dns shows the hostname to be cpanel14.fuitadnet.com(your ptr record). |
aldee 07-15-2005, 06:57 PM Originally posted by JonBlower
Nevermind. Ive sorted my problem now. I've basically found out what i wanted to do isnt possible. Cheers for your help all. I don't quite see your problem.
What's wrong with using<?
$hostname=`/bin/hostname`;
echo $hostname;
?>? |
adaml 07-15-2005, 08:12 PM I think he meant he wanted to find, the main hostname which is used for the DNS and on Dedicated servers. Letting the public know this is a security risk, as it opens up where your actually host is. Thats why when you set your hostname for the first and only time you make it long and complicated. |
fastduke 07-15-2005, 09:33 PM What is with the security crap? You guys have to be kidding me! Just by going to your website gives up where the host is. |
JonBlower 07-16-2005, 05:14 AM Fastduke, the jonnebob scenario is fictional, i should have made that clearer - sorry. I dont have the several servers for the project im working on, only the one.
http://www.jonnebob.co.uk/test.php
Ive managed to replicated what i want right at the bototm!
Cheers for all your help.
EDIT: Oh all of this will be "behind the scene" and none of the info for public use....not that that really matters anyway :) |
aldee 07-16-2005, 12:36 PM Huh? The reverse lookup of your domain's IP is cpanel14.fuitadnet.com, so there's certainly nothing secretive about that. |
kyfrow 07-16-2005, 03:28 PM I don't think he was asking for something like:
<?
$hostname=`/bin/hostname`;
echo $hostname;
?>
this should work fine:
echo $_SERVER['SERVER_NAME'];
I don't see what the huge confusion is about? heh
Cheers |
JonBlower 07-16-2005, 04:10 PM Originally posted by aldee
Huh? The reverse lookup of your domain's IP is cpanel14.fuitadnet.com, so there's certainly nothing secretive about that.
I never said there was. Thats what i was after though. The reverse lookup is of your domains ip is also the server hostname! |
aldee 07-16-2005, 06:03 PM Originally posted by JonBlower
I never said there was. Thats what i was after though. The reverse lookup is of your domains ip is also the server hostname! Setting your PTR records and changing your server's hostname doesn't have anything to do with each other. So no, it's certainly not (unless explicitly configured that way). |
|