Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
May 5, 2005, 11:24 pm
rate this thread
this may seem so lame to some, but I have exausted my brain trying to figure
this out, and therefore throw myself at the group:
basic breakdown of problem
$CurrentSite = $_SERVER["HTTP_HOST"];
<?php echo $CurrentSite ;?>
I only want the 'domain.com' part to display.. even if the host is
www.domain.com or whatever.domain.com
I just want to display the domain.com, am I right in thinking that regular
expressions would be the solution?
any assistance would save my sanity.
should work ok even with .co.uk :-)
however how would I extract just the domain part, in the case of
thisisasub.domain.com , www.domain.com or what if it was just domain.com the
script would have to be adaptable to all these scenarios.
That is my concern also DON. I will clarify further :
I wish to only have 'domain.com' in a variable.
that would mean that all subdomains like:
would all except the last one be stripped of the hostname and my variable
would contain only
It is proving to be a challenge ;-)
The best I could come up with is to break down your full name (like
my.ftp.do.main.com, in the case "do.main.com" will be the actual domain)
into separate elements using the "." as a delimiter. (see
http://us2.php.net/manual/en/function.explode.php on how to do this)
Then taking the last two elements - domain & com (no sense testing on just
the last as there are no domains named thusly) stringing them together with
the "." separating them. Then test it using the checkdnsrr function
(http://us4.php.net/checkdnsrr ) if it passes (won't in this case) then that
is your actual domain, if not add the next (going right to left) element to
the string with another "." as separator (now you would have do.main.com).
Do another checkdnsrr, and so on (though in this case it would pass the
test). This would be placed in a loop limited by the number of elements
Don Freeman wrote:
Good solution, except checkdnsrr() isn't implemented for Windows' PHP,
and it won't work for domains without a MX, or for valid subdomain
websites that do take their mail at the base domain. (eg.
www.johnsmith.domain.com could be a valid vhost who gets mail
@domain.com rather than @johnsmith.domain.com)
If you can't be 100% sure what the intended "base domain" is, then do
what many scripts do and simply make it a configuration option.
e.g. You can use wildcard DNS for domains such as
http://anything.goes.here.dev.basedomain.com , and there's no easy way to
know that "anything.goes.here" is the intended host part and
"dev.basedomain.com" is the base part just by looking at HTTP_HOST or
It's a solution, and propable the simpelest if you know how to compose
one (which is quite trivial so it's best to let you do your own research
to get the last 2 words seperated by a '.' :).
An other solution is to count the '.' and if there are more then 2
to take the substring from the second last '.' (strrpos/substr).