get domain name in php

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

what I started with is


the problem with this is that the result could be or

where I would like it to be 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 ?

Who is Participating?

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

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.

Nope, I think you have to cut it off in the code.
$domain = preg_replace("/^www\./", "", $_SERVER['HTTP_HOST']);
there is no such variable,
I think you should cut off the "www." in the code.
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.

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

The below will return '' for all of the following:

$domain = preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $_SERVER['HTTP_HOST']);
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   dpo you know if it will work on this domain name ?

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


  ..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.
No, that would just return
Brian BushSolutions ArchitectCommented:
So, you only want to cut off "www." if "www." is at the beginning?
That's an easy one:


$_domains = array(

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


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

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
have you tried simply using

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

Open in new window

$string = "";

$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

From novice to tech pro — start learning today.