get domain name in php

Im want to verify the domain name where my scripts are running

what I started with is

$_SERVER['HTTP_HOST']

the problem with this is that the result could be www.domain.com or domain.com

where I would like it to be domain.com always at all times only because I will be comparing the domain name with a field in the database to verify if they are allowed to be using my application anyone knows if there is a predefined variable for this or do i have to cut off the www.  in code ?

prowebinteractiveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

snoyes_jwCommented:
Nope, I think you have to cut it off in the code.
$domain = preg_replace("/^www\./", "", $_SERVER['HTTP_HOST']);
syrmaCommented:
there is no such variable,
I think you should cut off the "www." in the code.
gileze33Commented:
Just simply use
$domain = str_replace("www.","",$_SERVER['HTTP_HOST']);
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

snoyes_jwCommented:
Problem with str_replace is if your domain is something like 'mysiteonwww.com', you'll come away with just 'mysiteoncom'
prowebinteractiveAuthor Commented:
and what if its a subdomain, or there is no www.
snoyes_jwCommented:
if there's no www, no problem.

The below will return 'domain.com' for all of the following:
www.domain.com
domain.com
subdomain.domain.com
www.subdomain.domain.com

$domain = preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $_SERVER['HTTP_HOST']);
snoyes_jwCommented:
Here are a few other ways to do it:

preg_match("/[^.]*\.[^.]*$/", $_SERVER['HTTP_HOST'], $domain);
echo $domain[0];

$pieces = explode(".", strrev($_SERVER['HTTP_HOST']));
echo strrev($pieces[0] . "." . $pieces[1]);
prowebinteractiveAuthor Commented:
holy sweet jesus  nice but confusing, it works and thats what counts, I have tried it on every type of domain yet though  such as domain.qc.ca   dpo you know if it will work on this domain name ?
siliconbritCommented:


Just a quick pointer - I achieve the same using the IP Address of the server:

   $_SERVER['SERVER_ADDR']

  ..and I compare this with the IP address to which the domain name should resolve.

Basically, this avoids you having to do *any* additional comparison or string massaging.
snoyes_jwCommented:
No, that would just return qc.ca
Brian BushSolutions ArchitectCommented:
So, you only want to cut off "www." if "www." is at the beginning?
That's an easy one:

<?php

$_domains = array(
    "www.domain.qc.ca",
    "www.domain.com",
    "www.subdomain.domain.com",
    "domain.qc.ca",
    "www.domain.org",
    "domain.org",
    "subdomain.company.com",
    "mysiteonwww.com",
    "www.mysiteonwww.com",
    "www.www.com"
);

foreach ($_domains as $_domain) {
    echo preg_replace("/^www\./", "", $_domain);
    echo "<BR>\n";
}

?>

--brian
tolgaongCommented:
$subject="http://www.experts-exchange.com";
if (preg_match('/\\b(https?|ftp):\/\/www\.(?P<domain>[-A-Z0-9.]+)\\z/i', $subject, $regs)) {
      $result = $regs['domain'];
      }
echo($result);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zanus123Commented:
have you tried simply using


$theString = parse_url($_SERVER['HTTP_HOST']);
$theDomain = $theString['host'];
echo $theDomain;

Open in new window

zparksCommented:
<?php
$string = "kancrutwww.badrun.co.id";

$pos = strpos($string,"www");

$string = substr($string,($pos+4)); //$pos is the location for www, we have to add 4steps to get rid of the "www."

echo $pos."<br>";


echo $string;
?>

Open in new window


something like this?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.